File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile-time errror Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile-time errror" Watch "compile-time errror" New topic
Author

compile-time errror

shabbir ali
Greenhorn

Joined: Sep 02, 2006
Posts: 16
HI....

class a
{
public int i=10;
public int geti(){return i;}
}

class b extends a
{
public static void main(String args[])
{
System.out.println("the value of i "+ this.geti());
}
}


when i am comipiling above file it is saying this error......

E:\Dumps\progs>javac b.java
b.java:11: non-static variable this cannot be referenced from a static context
System.out.println("the value of i "+ this.geti());
^
1 error
.............

why it is giving compile-time error ....while the same program is given in
cathy seirra's (scjp study guide ) on page no 25...
please clarify it...
thank you.......
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hang on. Here
you have posted a different example from page 25 of the K&B book (SCJP 5.0). What version of the book has this code on page 25? Have you another version of the book with this code on page 25?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
bing marquez
Greenhorn

Joined: Aug 06, 2006
Posts: 11
hi shabbir,

A simple explanation about "this" is that it refers to the currently executing object. Since the main method is static--meaning it belongs to
the class and not to any instance of the class--it has no knowledge of
"this" so you cannot use this.geti() inside main().

But even if you remove "this" and simply call the method geti(),
since geti() is inherited anyway, it still will not compile. simply because geti() is not static and you cannot use non-static members in a static context.

So in order to compile and run the code, you have to make the method geti() static, which also follows that you have to make the variable i static as well.

The part about the code being in the K&B book, i'm not pretty sure about that..?

Hope this helps you.

[ September 03, 2006: Message edited by: bing marquez ]
[ September 03, 2006: Message edited by: bing marquez ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
From what I read in the SCJP 5.0 version of K&B, the authors have made it very clear that this code would not compile, because of the reasons bing has pointed out. To paraphase K & B "Static methods cannot access instance members because there is no instance" (Unless the static method creates an instance and use a reference to access the members of that instance)
Shang Lee
Greenhorn

Joined: Aug 17, 2006
Posts: 20
Hi Shabbir,

Or you can make an instance of either a or b inside main(), then you can use that instance to invoke geti()instead of making geti() and i static.


Although this will get the code to compile and run, it is considered a syntax trick. Try sticking to the other suggestion as it helps show you the important concept: Static methods cannot access non-static variables.
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
I can't find this code on page 25. Please tell us what page you found this code on.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The problem with your latter code example, Shang, is that by converting the instance members into class (static) members you have changed the whole meaning of the class a. Now i is shared by all instances and geti() cannot be overridden.

If class a has to retain its original meaning, then the only way to access the instance members is to access them via a reference to an instance of class a. That certainly is not a syntactical trick. Perhaps you are thinking of accessing a static member of a class through a reference to an instance?

Also in your latter code: the b.geti() would be better replaced by just geti() or a.geti() (Oh, why didn't Java enforce class names beginning with capital letters?)
[ September 03, 2006: Message edited by: Barry Gaunt ]
Shang Lee
Greenhorn

Joined: Aug 17, 2006
Posts: 20
Hi Barry,

Thank you for pointing them out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: compile-time errror