• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is it possible to invoke a non-static method without using a reference variable?  RSS feed

 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[size=12]Why is it possible to invoke a non-static method(i.e.setUpGui()) without using a reference variable?(see the picture) [/size]
The picture comes from the book named<Head First Java>.
go().PNG
[Thumbnail for go().PNG]
 
Tim Moores
Saloon Keeper
Posts: 4035
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just a useful shortcut for referring to the current object where the code runs. It's the same as writing "this.setUpGui()".
 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can I give an explanation that go() (i.e.an instance method)can access setUpGui()(i.e.an instance method)directly without using an object reference ,based on the rules in the next figure?
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
directly.PNG
[Thumbnail for directly.PNG]
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It helps to understand the name resolution process that the compiler uses to figure out what the program code is referring to. When the invocation only has MethodName, the compiler always starts from the smallest enclosing scope, then works its way out to wider scopes to try to find a matching method. If can't find any matching methods after looking through all the natural scopes, i.e. in the enclosing class and its superclasses, then it will try use any declared static imports to see if it can find a matching method that way.

You can read the relevant section of the JLS here: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.1 (see the first bullet point)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crystal Zeng wrote:
Can I give an explanation that go() (i.e.an instance method)can access setUpGui()(i.e.an instance method)directly without using an object reference ,based on the rules in the next figure?

In the case of an instance method accessing another instance method or instance variable without an explicit object reference, only the first rule applies: Instance methods can access instance variables and instance methods directly.

As Campbell explained, "directly without an object reference" is a bit misleading. To be more complete, that should be "directly without an explicit object reference" because the compiler will try to use the implicit "this" reference. That is, it will assume you are referring to a method of the current object, one that is declared in the enclosing class or inherited from one of its superclasses.
 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu Lacar
Thank you very much.
Your precise expression is the style I like best.( as accurate as math concepts).
Your link is also very useful to remove my confusion exactly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!