-3
class Name{

    String name;
    Name(String name){
        this.name = name;
    }
    void changeName(String name){
        this.name = name;

    }
    String getName(){
        return this.name;
    }

    void swap(Name other_Name_object){

        String temp;
        temp = other_Name_object.getName();
        other_Name_object.changeName(this.name);
        this.name = temp;

    }

    public String toString(){
        return this.name;
    }
}

I'm not sure why my code doesn't run on my java compiler. It has the same name as the file names.java

tddmonkey
  • 20,798
  • 10
  • 58
  • 67

2 Answers2

3

For any Java code to run, it needs a main method to tell the JVM where to start.

You would need to add this:

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

to your class.

Steve Smith
  • 2,244
  • 2
  • 18
  • 22
0

Use the following code and see your output. You need to have a main method in order to tell the JVM where to look for a start.

class HelloWorld {

  public static void main(String[] args) {
    Name n1 = new Name("John Doe");
    System.out.println(n1.getName());
  }

}

class Name {
  String name;
  Name(String name) {
    this.name = name;
  }
  void changeName(String name) {
    this.name = name;

  }
  String getName() {
    return this.name;
  }

  void swap(Name other_Name_object) {

    String temp;
    temp = other_Name_object.getName();
    other_Name_object.changeName(this.name);
    this.name = temp;

  }

  public String toString() {
    return this.name;
  }
}
CodeMonkey
  • 2,828
  • 1
  • 23
  • 32