Win a copy of Hello World this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

novice in java,please help!

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does the error message "non static method cannot refernced from a static context" means?

thanks in advance
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur response

what is the difference between static method and non-static method?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does the error mess "Exception in thread "main" java.lang.NoClassDefFoundError" means?

explanation plz..
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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) {...}
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
this is supposed to be a surprise, but it smells like a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!