Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

non-static Toolkit.getScreenSize()?

Status
Not open for further replies.

Sen7inel

IS-IT--Management
Feb 14, 2001
90
FI
Hi,

What's wrong here: If I have

class a {
void oneMethod { Dimension d = Toolkit.getScreenSize();
..
}
}

I get "non-static method gSS can't be referenced from a static context" compile error. Same happens for GraphicsEnvironment.getCenterPoint().
 
Duh, it seems I need to study the API docs more carefully. Obviously it works when I do:

GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();

Point ct = ge.getCenterPoint();

I'm still too thick (and beginner) to actually understand why this is like this. Anyone care to shed some light?
 
Have a look at:

A class method call looks like:
Code:
MyClass.someClassMethod();
An instance method call looks like:
Code:
myClassInstance.someInstanceMethod();
Further explanation from java.sun.com:

A method that is declared static is called a class method. A class method is always invoked without reference to a particular object. An attempt to reference the current object using the keyword this or the keyword super in the body of a class method results in a compile-time error. It is a compile-time error for a static method to be declared abstract.
A method that is not declared static is called an instance method, and sometimes called a non-static method. An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.

Hence, your error was caused by trying to use an instance method as a class method.
Hope this helps, Neil :)
 
Sorry, I went off on one. I'm not sure my answer is entirely correct :-(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top