• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling a non static method from static method  RSS feed

 
Dikshant Agarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that a non static method can not be directly refrenced from static method but in the example given below there is no such error.Why?

In above case a non static method display() is being invoked from static method invoke ()but it is not giving error.why?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dikshant Agarwal wrote:We know that a non static method can not be directly refrenced from static method but in the example given below there is no such error.Why?

Because you are not directly calling the non-static method; you are calling it on the object that the variable x refers to.

Think about this: If it were not at all possible to call a non-static method from a static method, then non-static methods would have no purpose at all. Programs always start at a static method, the main method; if you couldn't somehow call a non-static method from main, then it would never be possible to call any non-static method.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule really isn't that you cannot call a non-static method from a static method.
Because, as you can clearly see, you can.
The rule is that you cannot call a nnon-static method in static context. Which means, like stated above,
you cannot directly call a non-static method from a static one.
But you can call a non-static method on an object you are referring to, because
once you refer to an object you are no longer in a static context.
 
Steve Kedzie
Greenhorn
Posts: 22
Mac Netbeans IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use the fully qualified name to call a static method, as in Class.methodName(). Also, if you are trying to access static methods from within the same class, you can just use the methodName(). Your formatting and indentation is confusing.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think must is a bit strong since it is certainly possible to access visible static members of a class through a reference to an object. It is best practice to access them through the classname though.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume, Steve Kedzie, that your post originaly said
You must use the fully qualified name to call a static method, as in Class.methodName().
Please don't change posts like that after they have been replied to; it can make the replies look like nonsense.

Actually, that isn't the fully‑qualified name. The fully qualified name of a method would be
java.util.Arrays.sort(myArray);
… because it includes the packaage name.
You are right that it is poor practice to name static members by objectReferenceName.member. You can't do that with Arrays because it is uninstantiable. You can do this however, which is even worse:-
((java.util.Arrays)null).sort(myArray);

And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!