Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invocation of methods from static methods

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 questions:
1) If I understand it I can NOT invoke a non-static method from within a static method. I can only do that if the non-static method is invoked on a static reference. Is this the right thing?
If so, this code:


with this invocation from the command line:

java Elway 32


creates a compiler error because the invocation of the method go () (present in the main () which is a static method) within System.out.println () is done without using a static reference?




2) Given this code:


the result will be this:

343 341 340


But I did not understand why the first call (the one inside System.out.println ()) to tooth go to invoke the instance variable static while the second call (the one inside Knowing new (). DoIt) go to invoke the variable local defined inside the main ().
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not want that my post has not been read.
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
1) If I understand it I can NOT invoke a non-static method from within a static method. I can only do that if the non-static method is invoked on a static reference. Is this the right thing?


Almost, but not quite. You cannot invoke a non-static method from within a static method, without an instance. If you use a reference to an instance, it will work fine. Executing a non-static method, without an instance, will try to use the this instance -- which isn't available from a static context.

Henry

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use go() by itself. The first time you use it it's associated with an instance. The second time, it isn't so to fix it you would either associate it with an instance or make it static. Eclipse suggest making things static which makes a lot of beginners fix this kind of thing that way.

use either:

or


Just make sure all your Elways are referring to the same instance! (unless you don't want them to). Every new Elway() creates a new separate instance of an Elway.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me make a few other additions.

John Lerry wrote:with this invocation from the command line:

java Elway 32


creates a compiler error because the invocation of the method go () (present in the main () which is a static method) within System.out.println () is done without using a static reference?

The command line invocation is completely irrelevant here. The Elway class will not compile, because you invoke an instance method from a static method without an instance. So because the code doesn't compile, you don't have a class file and the command line invocation becomes moot. Even without this command line invocation, the code will still not compile!

John Lerry wrote:But I did not understand why the first call (the one inside System.out.println ()) to tooth go to invoke the instance variable static while the second call (the one inside Knowing new (). DoIt) go to invoke the variable local defined inside the main ().

First of all, it's very important to (learn to) use the appropriate Java terms (jargon). It makes communicating much more easier and clear, less chance of misunderstandings. So a little quick overview of all different variable types:var1 is a class variable (also known as a static field, a static variable,...)
var2 is an instance variable (also known as an instance field, instance data member, data member, field,...)
method1 is a class method (also known as a static method)
method2 is an instance method
var3 and var4 are method parameters (also known as parameters)
var3a and var4a are local variables
More info about class members (class variables and class methods) can be found here. And here you'll find more info on instance members (both instance variables and instance methods).

I have difficulties understanding your question about the Knowing class. Can you adjust your question using the appropriate Java jargon please?

Hope it helps!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Let me make a few other additions.

John Lerry wrote:with this invocation from the command line:

java Elway 32


creates a compiler error because the invocation of the method go () (present in the main () which is a static method) within System.out.println () is done without using a static reference?

The command line invocation is completely irrelevant here. The Elway class will not compile, because you invoke an instance method from a static method without an instance. So because the code doesn't compile, you don't have a class file and the command line invocation becomes moot. Even without this command line invocation, the code will still not compile!


The speech is clear to me (compile time error in line 7), however, I do not understand a thing. in this code:




I invoke the no-static method on a static reference. So, it is immaterial whether the reference is static or instance?


Roel De Nijs wrote:
John Lerry wrote:But I did not understand why the first call (the one inside System.out.println ()) to tooth go to invoke the instance variable static while the second call (the one inside Knowing new (). DoIt) go to invoke the variable local defined inside the main ().

First of all, it's very important to (learn to) use the appropriate Java terms (jargon). It makes communicating much more easier and clear, less chance of misunderstandings. So a little quick overview of all different variable types:var1 is a class variable (also known as a static field, a static variable,...)
var2 is an instance variable (also known as an instance field, instance data member, data member, field,...)
method1 is a class method (also known as a static method)
method2 is an instance method
var3 and var4 are method parameters (also known as parameters)
var3a and var4a are local variables
More info about class members (class variables and class methods) can be found here. And here you'll find more info on instance members (both instance variables and instance methods).

I have difficulties understanding your question about the Knowing class. Can you adjust your question using the appropriate Java jargon please?

Hope it helps!
Kind regards,
Roel



let's see if I can clarify the doubt alone.
Now I try to comment on the lines and tell me if they are good comments.

- line 8: within the System.out.println is called the final static variable (we can define a constant) "tooth" and then the print result will be 343.
- line 9: is defined as an final instance variable local (also a constant) that is initialized (this is necessary because it is local) with the value 340.
- line 10: we are trying to invoke the static method doIt. In theory, the call had to be made, as we talk about a static method, using the name of the class but also invoking the method using a temporary object it works good. At doIt method is passed the local instance variable that it will increases and print the value 341.
- line 11: prints the value of the local instance variable that is 340.


Is it correct?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:I invoke the no-static method on a static reference. So, it is immaterial whether the reference is static or instance?

If you want to access instance members (both instance variables and instance methods), you must have an instance (hence their name). It doesn't matter if you are inside a static method (or a static initializer block) or in an instance method. If you have an instance, you can access an instance member. This instance could be a local variable, an instance variable or (like in your TestClass example) a static/class variable.
To access static/class members (both static/class variables and static/class methods) you don't need an instance at all. You can simply invoke the method without having to create an instance first. Illustrated in this code snippet:Or you could invoke the calc method from another class as well. And although it's not needed to have an instance, you could use an instance to invoke this method as well. Both illustrated in this little program:

John Lerry wrote:Now I try to comment on the lines and tell me if they are good comments.

I will I repeat the code snippet here again, so we don't need to scroll every time

John Lerry wrote:- line 8: within the System.out.println is called the final static variable (we can define a constant) "tooth" and then the print result will be 343.

Almost correct! The final static/class variable tooth is declared (and initialized) on line2. It is indeed a compile-time constant. On line8 this value is printed and the result indeed is 343.

John Lerry wrote:- line 9: is defined as an final instance variable local (also a constant) that is initialized (this is necessary because it is local) with the value 340.

Again almost correct! There is no such thing as an instance variable local: a variable is either an instance variable or a local variable, but can't be both. So in this case it's a final local variable which is initialized with value 340. And it's indeed required if you want to use a local variable (if you don't use a local variable, you don't need to initialize it)

John Lerry wrote:- line 10: we are trying to invoke the static method doIt. In theory, the call had to be made, as we talk about a static method, using the name of the class but also invoking the method using a temporary object it works good. At doIt method is passed the local instance variable that it will increases and print the value 341.

It would have been completely perfect, if you would have used local variable instead of local instance variable.

John Lerry wrote:- line 11: prints the value of the local instance variable that is 340

If you would have used local variable instead of local instance variable, it would have been spot-on.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic