Reputation: 11
Basically I get the error message: "Error: Could not find or load main class Driver" Why Am I getting this error? This is my first instance working with classes, so I am unsure of my syntax in my code. Any help is appreciated, thanks.
public class Person {
private String name;//a variable that contains a person's name
private int age;//a variable that contain's a person's age
public class Driver{
public void main(String[] args )
{
String name1="John";
int age1=30;
Person person1= new Person();
person1.setName(name1);
person1.setAge(age1);
System.out.print(person1.getName());
System.out.print(person1.getAge());
}
}
//returns the age of a person
public int getAge(){
return age;
}
//returns the name of a person
public String getName()
{
return name;
}
//changes name of a person
public void setName(String s)
{
name=s;
}
//changes age of a person
public void setAge(int x)
{
age=x;
}
}
Upvotes: 1
Views: 365
Reputation: 159754
The JVM can't locate the main
method as specified by the JLS
The method main must be declared public, static, and void.
You need to make the inner class a top level class and make the main
method static
(since static methods can only belong to top level classes)
public class Driver {
public static void main(String[] args) {
...
}
}
Upvotes: 4
Reputation: 21961
You need to make inner class static
as well as main
method. You can't put static
method on instance inner class.
public class Person {
public static class Driver{
public static void main(String[] args) {
....
}
}
}
Upvotes: 1
Reputation: 93842
public void main(String[] args )
should be public static void main(String[] args )
Upvotes: 3