aspose file tools*
The moose likes Beginning Java and the fly likes Statics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Statics" Watch "Statics" New topic
Author

Statics

Leonardo Go
Greenhorn

Joined: Jun 13, 2004
Posts: 13
//main is static right ???

public class Answers{

static class Caps {

void write(){
System.out.print("Lordan Go");
}
}

public static void main(String[] args){
Caps s = new Caps();
s.write();
}
}

/*
if i change the static to ... not static..

non-static variable this cannot be referenced from a static context
Caps s = new Caps();
*/

Please explain .. thnx


Problems are made to be solved.
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
now thats an easy one .... look here ... u have made a class inside a class thats called a nested class ..... now see that u have declared this inner class as static ... right ... that means it is not associated with any object/reference ... it belongs to the class ....gottit ... now when it belongs to the class .... and not to any object ... how can u use "this" ...this refers to an object ...and the class doesn't belong to any object ...gottit ... even if u use "this" inside the main method ...u would get the same type of arror ... "this" points to an object ... and when u use "this" inside a static context ....the compiler says ..."man , what do u mean by this .... i am not with any object ... what do u mean by "this" ..."


Impossible is I M Possible
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Leonardo,

You might like to read the "Getting in Touch with your Inner Class" story in the JavaRanch Campfire Stories.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Statics
 
Similar Threads
runtime exception
Iam Getting runtime Error...(What is Down Casting)
Queries
JAVA GUI Problem
Words with capital letters