The moose likes Beginning Java and the fly likes Problem with static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with static methods" Watch "Problem with static methods" New topic

Problem with static methods

Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi friends,
Non static method cant be called from the static method that is true.But i have a done program below is it correct? If i do that what will be the problem

class palindrome{
static int a,b,c;
static void j(){
palindrome o=new palindrome();
void g(){
System.out.println(a+" "+ " "+b+" "+ " "+c );

public static void main(String args[]){

Please clarify my doubt friends
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

Well, you can just try to compile it and see if you get any errors...

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
I am not getting any errors!!
Rajiv Ranjan

Joined: Jul 05, 2007
Posts: 1
hey ,program is all correct and will have created instance of palindrome class in static method and then calling nonstatic method on that instance which is ok.actually the constraint on nonstatic method is because static method is only one for whole class and does not have any information about any particular (instance of class)object so if you call nonstatic method directly (for ex simply j()) static method does not on which object method is to be invoked.but in this program you have created the instance of palindrome class so static method now have reference of object on which the method is to be invoked.
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Thanks Rajiv
I agree. Here's the link:
subject: Problem with static methods
It's not a secret anymore!