Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is wrong with my code?

 
Venkata Raaman
Ranch Hand
Posts: 46
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am trying to write some code regarding static method/var access:
Here is the code:
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the current code there are few errors- There is no closing } for the class declaration, there is no return type in method z().
 
Jim Jonides
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you get that errors? Can you fix problems that Mohamed mentioned and let us know if you are still getting the same errors?
 
Venkata Raaman
Ranch Hand
Posts: 46
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Jonides wrote:How did you get that errors? Can you fix problems that Mohamed mentioned and let us know if you are still getting the same errors?

I refined the code: I still get a warning kinda but the program compiles: here is the code:

ERROR:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method y() from the type MystaticEx2

at MystaticEx2.main(MystaticEx2.java:24)
 
Jim Jonides
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can ignore the warning, Java allows you to call static method on an instance object. The error you got from the last line is as expected - you can not call instance method in a static method without using an instance object.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static - mean class level. main() method is a static method and y() is a non static method.
Static method can be referenced directly by it's name without any instance object in a Static method only.

A non static method would require an instance object to be invoked from a static method (for eg - main())

correct invokation code for y() would be

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its recommended to use the Class name for accessing static members.
 
Venkata Raaman
Ranch Hand
Posts: 46
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all....So In exam they do make sure those kind of questions (warnings) not shows up??just curious...!! OR they include those kind of questions as trick??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there would be questions which also check the warnings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic