can anyone explain me this concept that "static members of a class can access only static variables?" any simple code as an example to show this?

it takes you about ten seconds to write this yourself...
have you tried it yet?

don't think that it is my homework. i m really not getting any example.if u can help please help!!

it takes you about ten seconds to write this yourself...
have you tried it yet?

hey! one more question. static members can access only static members and variables. but how main() can access every instance whether it is static or not? please tell m not getting it

the answer would be ... it can't.
just you try and run this:

public static void main(String args[]){
  // call a non-static method from a static context
  // or at least try to.
  printSomething();
}

// non-static method
public void printSomething(){
System.out.println("see?");
}

what IS possible, is that you create an instance of the object in your static (main) method
and call the non-static methods from that instance, but, even though it's still calling a non-static method, it's no longer calling it from a static context.

public class Example{
public static void main(String[] args){
Example inst = new Example();
// since you're calling it using an instance of the class (inst) it's no longer being
// called in a static context.
inst.printSomething();
}

// non-static method
public void printSomething(){
System.out.println("TA DAAAH :)");
} 
}

the answer would be ... it can't.
just you try and run this:

public static void main(String args[]){
  // call a non-static method from a static context
  // or at least try to.
  printSomething();
}

// non-static method
public void printSomething(){
System.out.println("see?");
}

what IS possible, is that you create an instance of the object in your static (main) method
and call the non-static methods from that instance, but, even though it's still calling a non-static method, it's no longer calling it from a static context.

public class Example{
public static void main(String[] args){
Example inst = new Example();
// since you're calling it using an instance of the class (inst) it's no longer being
// called in a static context.
inst.printSomething();
}

// non-static method
public void printSomething(){
System.out.println("TA DAAAH :)");
} 
}

what u mean to say is that i m creating objects of class in main().it is non static and then i m calling a non static method from it.means we can call non static method from a non static object in a static method(main() here!).is this u r saying na ? right ?

Yes, if you have a "non-static" object ("instance") you can use that to call non-static methods. This is true whether it's all happening inside a static or non-static method.

Yes, if you have a "non-static" object ("instance") you can use that to call non-static methods. This is true whether it's all happening inside a static or non-static method.

but in static i cant directly access or call non static method or variables? m i perfectly right ?

Edited 4 Years Ago by gourav1: n/a

Not directly, no. You need to get an instance through which to access them.

you are really great i must say. this thread is solved.thanks

This question has already been answered. Start a new discussion instead.