1

I am trying to learn how constructors work and I have been trying to debug this simple java program but I cannot get it to run. Eclipse simple refuses to acknowledge its presence and just runs an earlier project. Any ideas would be very gratefully received - I am struggling to see what I have done wrong.

package timber;



public class Person {

 private String firstName;
 private String lastName;
 private String address;
 private String username;


 public Person(String personFirstName, String personLastName, String personAddress, String personUsername)
 {
 firstName = personFirstName;
 lastName = personLastName;
 address = personAddress;
 username = personUsername;
 }


 public void displayPersonDetails()
 {
 System.out.println("Name: " + firstName + " " + lastName);
 System.out.println("Address: " + address);
 System.out.println("Username: " + username);
 }





 }

I then have a second class that contains the main method

package timber;

 public class PersonExample {



     public void main(String[] args) {

     Person dave = new Person("Dave", "Davidson", "12 Main St.", "DDavidson");
     dave.displayPersonDetails();

     }
     }
  • You have to check what you are setting in the green run button and change it to your current project. also make sure that you are saving your project before you run it – Youcef LAIDANI Dec 25 '19 at 18:49
  • In this rare instance, a screenshot of your eclipse would be helpful. Try "Run > Run As > Java Application" – Elliott Frisch Dec 25 '19 at 18:49

4 Answers4

2
could you please add static in main method :-

public static void main(String[] args) {

     Person dave = new Person("Dave", "Davidson", "12 Main St.", "DDavidson");
     dave.displayPersonDetails();

     }
Sarjit
  • 799
  • 7
  • 9
0

Your main method always needs to be static.

See Why is the Java main method static?

Replace,

public void main(String[] args)

with

public static void main(String[] args)
Adwait Kumar
  • 1,552
  • 10
  • 25
0

Your main method needs to be 'static'. Why? If you want to call a method in Java, there are two ways

  1. Create an instance of class and use that object to call it's methods
  2. Make a method 'static' so that you can call the method using the class name and no instance creation needed.

The 'main' method, if non-static (like in your case) you have to create an instance of it's class to call. But, where would you create an instance of that class, where your main method is residing? Create another class and call from there? and create yet another class to call this class? It will never end. I mean there has to be a starting point right?

By giving the method the name 'main' and by adding the keyword 'static' in it's signature, you help JVM call that method without the need to an create instance.

Just change

public void main(String[] args)

to

public static void main(String[] args)
rakesh mehra
  • 618
  • 1
  • 9
  • 21
0

Everything is fine you have just missed the static keyword . Instead of

public void main(String [] args)

Use

public static void main(String[] args)