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

error: non-static method ... cannot be referenced from a static context

 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I would like to know something that is not clear enough for me.

Let's see the snippet:



When compiling I get the error: "error: non-static method foo(int) cannot be referenced from a static context"

but I'm not sure if the message is referring to main static method or "out" in println().

Please, could you help me?

Thanks in advance.



 
Tim Cooke
Sheriff
Pie
Posts: 3209
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "static context" in question here is the 'public static void main()' method. You are calling non-static method foo(int) from static method main(String[]), which is not allowed. Do you know why that's not allowed?
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think is because I need an instance of Pepe class if I want to call the non-static method....
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main is a static method
and you are invoking non static method from static method

It will result in Compiler error,until you create an instance of class and invoke foo() with the help of that instance


And in System.out.println()
System is a class in java.lang
out is a static variable, of PrintStream type,declared within System
println() method in PrintStream
 
Tim Cooke
Sheriff
Pie
Posts: 3209
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:I think is because I need an instance of Pepe class if I want to call the non-static method....

Correct. To remind myself of this, I like to use the terminology 'instance' methods to refer to non-static methods.
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your quick answers.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:I think is because I need an instance of Pepe class if I want to call the non-static method....

Spot-on!

A method declared without the static keyword is called an instance method. The name of such a method tells the story: you need an instance to invoke this method. A method declared with the static keyword is a class (or static) method. And again tells the name of such a method everything you need to know: you need a class to invoke this method. So that means you can invoke a class (static) method from an instance (non-static) method (context); but you can't (directly) invoke an instance method from a class (static) method (context), as an instance is required to invoke an instance method.
And for completeness: the same rule applies to instance fields and class (static) fields as well. You can access a class (static) from an instance (non-static) context, but you can't (directly) access an instance field from a class (static) context (as an instance is required to access an instance field).
I have used the word "context", because it's more than only fields and methods. An instance (non-static) context is (obviously) instance fields and instance methods, but also constructors and instance initializer blocks. And likewise a class (static) context is (obviously) class (static) fields and class (static) methods, but also static initializer blocks.

Now let's see if you fully understand all the above. Time for a pop quiz! Which of the following lines will produce a compiler error?

Hope it helps!
Kind regards,
Roel
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Roel, here we go...

line2 is wrong -> trying to call instance method straight without object.

lines 3 and 4 ; ok, there is an instance for nonstatic boo2 and for boo1 should be better JavaFun.boo1, but fun.boo1 is ok.

for lines A, B, C and D is ok, beacause if I am accessing to foo2() is cause there is and previously created instance of JavaFun and from instance methods we can access to both static and nonstatic.

Is it right?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:Is it right?

Both the answer and the explanation is absolutely spot-on!

Now let's have another pop quiz. What's the output of the following code snippet?
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answer:


foo1() should be called as: StillJavaFun.foo1() but does not matter if there is no object.

foo2: need an object to be called and there is no "new" keyword anywhere. Then We get Runtime error
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:foo1() should be called as: StillJavaFun.foo1() but does not matter if there is no object.

You should be more specific: do you get output or a runtime exception or maybe a compiler error?

Alfonso Saenz wrote:foo2: need an object to be called and there is no "new" keyword anywhere. Then We get Runtime error

Exactly! A NullPointerException will be thrown at runtime.
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the output for foo1 is ok. That is: "in foo1 (class)".

By the way: could you give me answer to my other today's question on topic: "Are static variables inherited through constructors??"

Thank you very much Roels. You are so fast answering...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfonso Saenz wrote:the output for foo1 is ok. That is: "in foo1 (class)".

Spot-on again! So for a class (static) method only the type of the reference variable is important. If the actual object (denoted by this reference variable) is null, it doesn't matter at all when invoking a static method, the method is still being invoked. But for an instance method, it really matters! If the reference variable is null, you'll get a NullPointerException at runtime.

Alfonso Saenz wrote:By the way: could you give me answer to my other today's question on topic: "Are static variables inherited through constructors??"

It's on my list to have a look at later today.
 
Alfonso Sanz
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Roel De Nijs !!!
You are the master!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic