Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is wrong with my code?

 
Ranch Hand
Posts: 50
VI Editor Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i am trying to write some code regarding static method/var access:
Here is the code:
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its recommended to use the Class name for accessing static members.
 
Venkata Raaman
Ranch Hand
Posts: 50
VI Editor Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there would be questions which also check the warnings.
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic