File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Statics" Watch "Statics" New topic


Leonardo Go

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();

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

Joined: Dec 10, 2001
Posts: 7023

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:
subject: Statics
It's not a secret anymore!