wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is wrong with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is wrong with my code?" Watch "What is wrong with my code?" New topic
Author

What is wrong with my code?

Venkata Raaman
Ranch Hand

Joined: Mar 26, 2010
Posts: 45

Hi i am trying to write some code regarding static method/var access:
Here is the code:
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

With the current code there are few errors- There is no closing } for the class declaration, there is no return type in method z().


Mohamed Sanaulla | My Blog
Jim Jonides
Ranch Hand

Joined: Oct 12, 2010
Posts: 42
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?


Devil is in the detail.
Venkata Raaman
Ranch Hand

Joined: Mar 26, 2010
Posts: 45

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

Joined: Oct 12, 2010
Posts: 42
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

Joined: Feb 29, 2008
Posts: 1076
    
    1

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



Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Its recommended to use the Class name for accessing static members.
Venkata Raaman
Ranch Hand

Joined: Mar 26, 2010
Posts: 45

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

Yes, there would be questions which also check the warnings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is wrong with my code?
 
Similar Threads
label continue
problem in System.out.println
ques
Arguments & Parameters
x++ , ++x