Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Finding where variable is defined and where referenced

  1. #1
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Finding where variable is defined and where referenced

    Is there a way to select a variable in your code and ask the IDE to tell you where it is defined and where it is referenced?

    There are many students that have shadowed variables. Is there a way to use the IDE to find the variables that are hiding variables at larger scope.

    Why doesn't the IDE warn you of this?

    When do you ever want to have more than one variable with the same name at different scope levels?

    Thanks,
    Norm

    Also posted at: Finding where variable is defined and where referenced


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Finding where variable is defined and where referenced

    In Eclipse:

    To find declaration:

    right click on the variable/method/class/etc. and choose "Open Declaration" (shortcut key is F3).

    To find usage:

    right click on the variable/method/class/etc and choose "References" and then in what context you want to find it's usage (in the current project, hierarchy, or workspace, or a specific working set).

    You can also do a "Java search" (click the little arrow next to the search icon and choose "Java search") to search for a variety of different fields, methods, classes, etc. in whatever context you want and even how Eclipse thinks you're using it at that time.

    To enable Eclipse's JDT to determine what to do about name shadowing in various contexts (ignore, warn, or throw a compiler error):

    Windows > Preferences > Java > Compiler > Error/Warnings > Name Shadowing and Conflicts

    The available items are:
    Field declaration hides another field or variable (this is probably one you want to change)
    Local variable declaration hides another field or variable (this is probably one you want to change)
    Type parameter hides another type
    Method does not override package visible method
    Interface method conflicts with protected 'Object' method

    Note that these settings can be changed on a project-by-project basis, too.

    Right click on the project in the Package Explorer, then choose:

    Properties > Java Compiler > Errors/Warnings > Enable project specific settings

    I tend to use shadowing in constructors and setters. Mostly it's because often times I use Eclipse's automated generate getter/setter and generate constructor using fields functionality, but also because then I can quickly identify which method argument should match up with which field in my class.
    Last edited by helloworld922; September 11th, 2011 at 06:31 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Finding where variable is defined and where referenced

    Thanks. I was looking for something to pass to an OP when they are having shadowing problems that would show them the problem.

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Finding where variable is defined and where referenced

    just one tip: select a variable name, then press Ctrl + K will go through all occurrences of that variables.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Finding where variable is defined and where referenced

    Which IDE(s) does that work in?

    Does it tell you which ones are shadowed?

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Finding where variable is defined and where referenced

    Quote Originally Posted by Norm View Post
    Which IDE(s) does that work in?

    Does it tell you which ones are shadowed?
    Hi, I'm talking about Eclipse. I didn't mean it shows shadowed variables, just easily find occurrences (including variables and shadowed ones).

    java memory
    Last edited by namhm; December 4th, 2011 at 06:53 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Finding where variable is defined and where referenced

    The problem is the shadowed variables NEED to be marked with a warning. If you know to search for a variable to see if its shadowed then you won't need the warning.

  8. #8
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Finding where variable is defined and where referenced

    Quote Originally Posted by Norm View Post
    The problem is the shadowed variables NEED to be marked with a warning. If you know to search for a variable to see if its shadowed then you won't need the warning.
    Okay guy, I was missing the point.

    java memory
    Last edited by namhm; December 4th, 2011 at 06:55 PM.

Similar Threads

  1. How To Refresh Referenced Libraries In Eclipse ?
    By rajeev.sjsu in forum Java IDEs
    Replies: 8
    Last Post: September 11th, 2011, 02:28 PM
  2. Already defined?
    By SRBuckey5266 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 20th, 2011, 09:15 AM
  3. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 10:23 PM
  4. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  5. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM