Reputation: 21
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!
Upvotes: 2
Views: 1178
Reputation: 4264
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);
}
}
Upvotes: 0
Reputation: 250
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.
Upvotes: 0
Reputation: 5514
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
Upvotes: 3
Reputation: 1301
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 :-)
Upvotes: 0