2

I just started Java course in college, I'm trying to understand the concept of OOPs so I wrote this program:

package Lamp;
import java.util.*;

    public class Lamp {
    public Scanner input= new Scanner(System.in);
        boolean state;
        String color;

        public Lamp() {
            state = false;
            color = "Blue";
        }

        public boolean toggleState() {
            if (state == false) {
                state = true;
            }
            if (state == true) {
                state = false;
            }
            System.out.println("State is now: " +state);
            return state;
        }

        public String chooseColor(){
            System.out.println("Please choose a new color");
            color= input.nextLine();
            System.out.println("Color is now: " +color);
            return color;
        }

        void main(){
        Lamp L1= new Lamp();
        System.out.println("State is now: " +state);
        System.out.println("Color is now: " +color);

        L1.toggleState();
        L1.chooseColor();

        System.out.println("State is now: " +state);
        System.out.println("Color is now: " +color);

        }
    }

The problem is that every time I try to run the program, NetBeans says that it can't find the main class which is Lamp.Lamp I'm using the concept of packagename.classname, but it keeps putting the same thing.

Thanks in advance!

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78
Mumbzi
  • 21
  • 4
  • 3
    Your `main` has the wrong signature. It should be `public static void main(String[] args)`. See https://docs.oracle.com/javase/tutorial/getStarted/application/ – bradimus Nov 03 '16 at 14:27

4 Answers4

3
void main(){

should be changed to

public static void main(String[] args) {

public makes it visible.

static makes it possible to invoke the method without constructing the object first.

The explanation for why this is required is in the link mentioned by @bradimus

dubes
  • 5,324
  • 3
  • 34
  • 47
  • 1
    Nope. `public static void main()` is not correct. Try `public static void main(String[] args)` – bradimus Nov 03 '16 at 14:31
  • 1
    Thanks! I saw your comment on the post just after I posted my answer. Added your link to the answer as well! – dubes Nov 03 '16 at 14:32
0

Is it perhaps a similar problem to this?

Netbeans - Error: Could not find or load main class

Try cleaning and recompiling the project. Sometimes an older version of a program gets stuck in the cache and the new one can't run.

Also, try adding a public declaration public static void main(String[] args) to the main method at the end of your program and see if that helps.

Community
  • 1
  • 1
Curtis White
  • 250
  • 3
  • 13
  • 1
    `Also, try adding a public declaration ...`, well this should be the first line of your answer actually ;) That will help – AxelH Nov 03 '16 at 14:39
0

Right click your project -> click on properties -> click run -> browse -> add your main class.

Also you shoud use:

public static void main(String[] args) {

as your start for a Main method.

Hope that this helps :-)

Christian Moen
  • 1,253
  • 2
  • 17
  • 31
  • 1
    The main problem is the lake of the main method, not the way to start the application. Run on the specific java file would work to. – AxelH Nov 03 '16 at 14:40
0

first of all your main() is wrong, it should be public static void main(String args[]) then you need to declear your variable as static static boolean state; static String color;

check your code

package Lamp;
import java.util.*;

    public class Lamp {
    public Scanner input= new Scanner(System.in);
        static boolean state;
        static String color;

        public Lamp() {
            state = false;
            color = "Blue";
        }

        public boolean toggleState() {
            if (state == false) {
                state = true;
            }
            if (state == true) {
                state = false;
            }
            System.out.println("State is now: " +state);
            return state;
        }

        public String chooseColor(){
            System.out.println("Please choose a new color");
            color= input.nextLine();
            System.out.println("Color is now: " +color);
            return color;
        }

       public static void main(String args[]){
            Lamp L1= new Lamp();
            System.out.println("State is now: " +state);
            System.out.println("Color is now: " +color);

            L1.toggleState();
            L1.chooseColor();

            System.out.println("State is now: " +state);
            System.out.println("Color is now: " +color);

        }
    }
Sangram Badi
  • 4,054
  • 9
  • 45
  • 78