This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

What is wrong with my code?  RSS feed

 
Ranch Hand
Posts: 50
Fedora Java VI Editor
  • 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:
 
Bartender
Posts: 3185
34
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().
 
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: 50
Fedora Java VI Editor
  • 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.
 
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
Bartender
Posts: 3185
34
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: 50
Fedora Java VI Editor
  • 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
Bartender
Posts: 3185
34
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!