aspose file tools *
The moose likes Beginning Java and the fly likes novice in java,please help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "novice in java,please help!" Watch "novice in java,please help!" New topic
Author

novice in java,please help!

Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
what does the error message "non static method cannot refernced from a static context" means?

thanks in advance
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
It means exactly what it says ;-)

For example, you can't do something like this.

public class MyClass {
public void myNonStaticMethod() {
}

public static void main (String[] args) {
myNonStaticMethod(); // This line will cause a compiler error.
}

}

Simply marking the myNonStaticMethod as 'static' would take care of the above issue.


-Rich, SCJP 1.4
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might think of static members as "class" members, and non-static members as "instance" members.

A class can be loaded simply by a call to one of its static members, but that (in itself) does not create an instance. Within a static context, there is no guarantee that any instances exist, so non-static (instance) members cannot be referenced.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ashish Chande
Ranch Hand

Joined: Nov 26, 2000
Posts: 51
When we call a non static method from static method, it has to be associated to an object if it's not associated to an object, compiler will throw an error.


consider the example


It will throw an error. if we mark method A declaration as static, the code will compile. the other way is create an instance and call methodA using dot operator.

Ashish
Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
Thanks for ur response

what is the difference between static method and non-static method?
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
static methods are those belonging to a class(Blueprint for an object) and not a particular instance....

whereas the nonstatic methods are associated with a particualr instance ...

hope u got that.


Life is Beautiful Learn to See it.........
Wasim Ayoubi
Ranch Hand

Joined: Nov 21, 2004
Posts: 46
Hi there

static methods can be called without instantiating an object of the class they belong to. These methods usually do some general-purpose staff.

MyClass.doSomeThing();

non static methods are methods that usually perform logic on the caller object, you have to instantiate an object then call these methods by object reference

myObject.doSomeThing();

Regards

[ July 13, 2005: Message edited by: Wasim Ayoubi ]
[ July 13, 2005: Message edited by: Wasim Ayoubi ]

Wasim Ayoubi<br />SCJA1.0(BETA), SCJP1.4, SCWCD1.4<br />Next ???
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

non static methods are methods that usually perform logic on the caller object
There's an argument to be made that if the method *doesn't* do anything with the instance, it *should* be declared static.
Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
what does the error mess "Exception in thread "main" java.lang.NoClassDefFoundError" means?

explanation plz..
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jenny raj:
what does the error mess "Exception in thread "main" java.lang.NoClassDefFoundError" means? ...

You are trying to execute a java class file that has no main method (with the appropriate signature)...

public static void main(String[] args) {...}
Sean Magee
Ranch Hand

Joined: Aug 23, 2004
Posts: 69
For your static question, i htink this is a good way to put it.

Beginners have a hard time understanding programming terms, especially when you try to define them to the begiiner with more programming terms.

Best way to think about it is,

STATIC means that it stays put. so

if you have


SO the Static method stays put, the methodX is created along with class A.
Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
package one;
class alpha
{
private int iamprivate=1;
protected int iamprotected=2;
public int iampublic=3;
int iampackage=4;

private void privatemethod()
{
System.out.println("i am private method");
}
protected void protectedmethod()
{
System.out.println("i am private method");
}
public void publicmethod()
{
System.out.println("i am protected method");
}
void packagemethod()
{
System.out.println("i am package method");
}

public static void main(String args[])
{
alpha a=new alpha();
a.privatemethod();
a.protectedmethod();
a.publicmethod();
a.packagemethod();

System.out.println("iamprivate:"+ a.iamprivate);
System.out.println("iamprotected:"+ a. iamprotected);
System.out.println("iampublic:"+ a.iampublic);
System.out.println("iampackage:"+ a.iampackage);
}
}


the above code gives the execution error "Exception in thread "main" java.lang.NoClassDefFoundError: one/alpha"

i created a directory called one and saved the file alpha and it compiled without any error mess and doesn't execute

couldn't figureout where i am going wrong

help needed
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

First, when you post code, please use the Code Tags.

Second, the complete error message gives you a clue what the problem is. "Exception in thread "main" java.lang.NoClassDefFoundError: alpha (wrong name: one/alpha)..."

Because it's in a package, execute this by typing...

java one.alpha
 
 
subject: novice in java,please help!