File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is "non-static method" vs "static context" ?" Watch "What is "non-static method" vs "static context" ?" New topic
Author

What is "non-static method" vs "static context" ?

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi All.
Sometimes, when I try to add a simple method to exisitng codes, JAVA would give me this error :
non-static method compressedSize(int) cannot be referenced from a static context :
int cmpSize = compressedSize(cmpSize);
Can someone explain to me what this means please ?
TIA


humanum errare est.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
The concept of static is usually confusing for newbies, I know I was initially confused when I started to learn Java.
The simple answer is: That error will occur if a block of code in a static method attempts to access a non-static variable (e.g. instance member) or call a non-static method.
A brief explanation:
Static fields and methods are part of the generic class, i.e. part of the "template" that specific object instances are created from. Non-static fields/methods are associated to a specific instance, or object. So, static fields/methods have no way of accessing the members of a specific instance because they can't know which instance you are refering to.
Hope that helps!


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
non-static == there is a "this" object
static == there isn't a "this" object


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
wood burning stoves
 
subject: What is "non-static method" vs "static context" ?