i am newbie to java.i am trying to run a program to show "static method" and "static variable ".my code is below.i am compling it on netbeans 7.01 and everytime i am getting the error.:
"main method not found in class static_demo.Static_demo, please define the main method as:
public static void main(String[] args)"
please help me in executing the code.

package static_demo;
public class Static_demo {
int a=10;
static int b=20;
static void show()
{
    System.out.println("the value of b:"+b);
}
void anothershow()
{
    System.out.println("value of a"+a);
}
}
     class static_demo1
    {
    public static void main(String[] args) 
    {
        
        x1.show();
        x1 v=new x1();
        v.anothershow();
    }

    }

try putting your main method into the Static_demo class, and remove the inner class where you've actually put it in.

Apart from what stultuske said , I think you are executing static_demo class. Instead run static_demo1 class. Also you need to create object of static_demo in main(). What is x1 ? You should be calling it as static_class.show().

Apart from what stultuske said , I think you are executing static_demo class. Instead run static_demo1 class. Also you need to create object of static_demo in main(). What is x1 ? You should be calling it as static_class.show().

since it would be in the same class file, he could just use

show();

for the anothershow() method, he should indeed create an instance of Static_demo instead of an instance of a (as far as we know not existing) x1.

as for the other one, don't say: apart from -> that means, do both.
if he moves the main method to the Static_demo class and then tries to run the static_demo1 class, he would get exactly the same error, since: the main method will not be found

since it would be in the same class file, he could just use
Java Syntax (Toggle Plain Text)

show();

Yes true

as for the other one, don't say: apart from -> that means, do both.
if he moves the main method to the Static_demo class and then tries to run the static_demo1 class, he would get exactly the same error, since: the main method will not be found

Well thanks for correcting me. You are right ,'apart from' is misleading. But anyways you posted what I actually wanted to convey :)

thank you very much for the reply.
Stultsuke .i have done whatever u have suggested .but the same error is coming.
is there problem with my netbeans?
its 7.0.1
whenever i m writing any program on it ,it is showing the same error ,"Could not find or load main class"
i m new to java ,i cant understand what is the error.
please suggest me.

can you post your current code?

thank you very much for the reply.
Stultsuke .i have done whatever u have suggested .but the same error is coming.
is there problem with my netbeans?
its 7.0.1
whenever i m writing any program on it ,it is showing the same error ,"Could not find or load main class"
i m new to java ,i cant understand what is the error.
please suggest me.

hmm well as far as i see the problem is how you are doing your main method, beacuse in your code it is placed wrongly. a standard java program should be like this:

package javaapplication14;

public class JavaApplication14 {

    private static void printhello() {
        System.out.println("Hello");
        System.exit(0);
    }

    public static void main(String[] args) {
        printhello();
    }
}

use that code and see if it will run which it should if not your netbeans is messed!

also if you want to add another class it should be done like this:

package javaapplication14;

public class JavaApplication14 {

    private static void printhello() {
        System.out.println("Hello");
    }//end of printhello()

    public static void main(String[] args) {
        printhello();
        printbye.bye();//calling method in printbye class
        System.exit(0);
    }//end of main

    public static class printbye {
        
    private static void bye() {
        System.out.println("Bye");
    }//end of bye()
    
    }//end of printbye class
}//end of JavaApplication14 class

please note that my main class 'JavaApplication14' contains the main method and not the 'printbye' class. A java app may only have one main method in its main class, it cant have a main class with a sub class that holds the main method! main method must be present in you main class. also not all classes are enclosed within my main class.

Also , the class that contains your main() class should be your filename. Execute that class then.

mmm... the last two posts didn't really give any advice that wasn't given already.

Since the OP said he/she did all that, and it still doesn't work:

@OP: if you still have the problem, could you post the code you have now?

//import java.lang.*;
package static_demo1;
    public class Static_demo1 {
    int a=10;
    static int b=20;
    private static void show()
    {
    System.out.println("the value of b:"+b);
    }
     private void anothershow()
    {
    System.out.println("value of a"+a);
    }
    }
    class static_demo2
    {
    public static void main(String[] args)
    {
     
    show();
    static_demo2 v=new static_demo2();
    v.anothershow();
  
}
    }

thank-you everyone for helping me.Stultsuke i still have the same problem.
this is my present code .its my first static example.when i am running it it is giving the error:"Main method not found in class static_demo1.Static_demo1, please define the main method as:
public static void main(String[] args)"
if you have another example to show static method give suggest me.

look at your classes... you have the main method defined in the class that is not the main...

you class structure should look like this:

//import java.lang.*;
package static_demo1;

public class Static_demo1 {

    private static void show() {
    }

    public static void main(String[] args) {
    }
}

class static_demo2 {

    public void anothershow() {
    }
}
commented: beaten to the punch :) +12

you still have not done what I suggested. the problem is, your compiler searches for the main method within the public class Static_demo, but it is not inside that class.
move the main method to the public class

class static_demo2
{
public static void main(String[] args)
{
 
show();
static_demo2 v=new static_demo2();
v.anothershow();
 
}
}

move the main method outside of this inner class, like this:

public static void main(String[] args)
{
 
show();
static_demo2 v=new static_demo2();
v.anothershow();
 
}
class static_demo2
{

}

and, you'll need an anothershow method in your inner class.

commented: It takes a man to admit when they got beat ;) +4

if you have another example to show static method give suggest me.

i'd also just like to say, the problem you are having is not about static methods...
Static means that only one of it can exist. main() method will always be static as we cant have two main methods in a single class.
The problem you are dealing with is that you have put your main method in a sub class and not the main class. the main class is defined by the name of the file which the source code is present...your main class is: 'Static_demo1' where as your sub class is: 'static_demo2' and that is the class in which your main() is present i hope this helps to understand your problem better.

//import java.lang.*;
package static_demo1;
    public class Static_demo1 {
    int a=10;
    static int b=20;
    private static void show()
    {
    System.out.println("the value of b:"+b);
    }
     private void anothershow()
    {
    System.out.println("value of a"+a);
    }
    }
    class static_demo2
    {
    public static void main(String[] args)
    {
     
    show();
    static_demo2 v=new static_demo2();
    v.anothershow();
  }

}

this is my present code.i m not able to get the solution to the error"main class not found".as i newbie to java,i want to do an static example .so please help me

//import java.lang.*;
package static_demo1;
    public class Static_demo1 {
    int a=10;
    static int b=20;
    private static void show()
    {
    System.out.println("the value of b:"+b);
    }
     private void anothershow()
    {
    System.out.println("value of a"+a);
    }
    }
    class static_demo2
    {
    public static void main(String[] args)
    {
     
    show();
    static_demo2 v=new static_demo2();
    v.anothershow();
  }

}

this is my present code.i m not able to get the solution to the error"main class not found".as i newbie to java,i want to do an static example .so please help me

your show() and anothershow() methods should be public and not private or else your code in static_demo2 cannot access them. also this here :

static_demo2 v = new static_demo2();

should be:

Static_demo1 v=new Static_demo1()

the reason is because anothershow() is located in the class Static_demo1 and not static_demo2.

also accessing your show() method like this

show();

does not work as its not located in the static_demo2 class it should be:

Static_demo1.show();

and lastly your main method in static_demo2 should be moved too Static_demo1 class, as thats your main class hence the error message main method not found in Static_demo1-thus eliminating the use for static_demo2.. if you would like to retain static_demo2, then move all the methods located in Static_demo1 to static_demo2. then move the main method in static_demo2 to Static_demo1 and rename the names in main method from Static_demo1 to static_demo2.

I cant give you the code but heres some guideline after all the editing your structure should resemble this:

public class Static_demo1 {

    public static void main(String[] args) {
       static_demo2.sho..........
       static_demo2 v=......
       v.ano.......
       
    }
}

class static_demo2 {

    int y;
    static int x;

    public static void show() {
    }

    public void anothershow() {
    }
}
//import java.lang.*;
package static_demo1;
    public class Static_demo1 {
    int a=10;
    static int b=20;
    private static void show()
    {
    System.out.println("the value of b:"+b);
    }
     private void anothershow()
    {
    System.out.println("value of a"+a);
    }
    }
    class static_demo2
    {
    public static void main(String[] args)
    {
     
    show();
    static_demo2 v=new static_demo2();
    v.anothershow();
  }

}

this is my present code.i m not able to get the solution to the error"main class not found".as i newbie to java,i want to do an static example .so please help me

we have repeatedly told you what the problem is and (with code examples) how to solve it. try, if need be, step by step to do what we suggested.

cOrRuPtG3n3t!x was friendly enough to, yet again, repeat it one more time, now please, be so kind as to read his post and do as he suggests, step by step, so you can see the differences in result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.