-6

I am new to Java and to programming and i have one problem. My code compile , but give me Error: Could not find or load main class PatienTest. Can you explain me where is my mistake and what i am doing wrong. Here is my code :

class Patient {

    private String name;
    private int age;
    public String newName;

    public String getName() {
        return name;
    }

    public void setName() {
        name = newName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int newAge) {
        age = newAge;
    }
}

class PatientTest {

    private Patient pat;

    public boolean isChild() {
        return (pat.getAge() < 15);
    }

    public void main(String[] args) {
        System.out.println(isChild);
    }
}


class PatientTest {

    private Patient pat;

    public boolean isChild() {  
        return (pat.getAge() < 15);
    }
    public  void main(String[] args) {
        System.out.println(isChild);
    }
}
pistou
  • 2,799
  • 5
  • 33
  • 60
Spongi
  • 501
  • 3
  • 10
  • 19
  • Do you have the two classes in the same file ? – DamCx Dec 01 '16 at 10:14
  • you missed static for main method – Rajashekhar Dec 01 '16 at 10:16
  • 1
    When i make it static shows this error : non-static variable isChild cannot be referenced from a static context System.out.println(isChild); ^ 1 error [Finished in 0.8s with exit code 1] – Spongi Dec 01 '16 at 10:18
  • @Berger I took care of it too show the missing bracket. Not sure where `PatientTest` should fit, it isn't public so it could be in `Patient`. But `public void main(String[] args) {` need to be static : `public static void main(String[] args) ` and in it, create a `PatientTest` instance to work call isChild `new PateintTest().isChild(); – AxelH Dec 01 '16 at 10:18
  • No , the classes are not in the same file , but in the same folder – Spongi Dec 01 '16 at 10:19
  • Yes. But it is required to have main method with public static and void keywords. Make instance variable and method as static. – Rajashekhar Dec 01 '16 at 10:20
  • The signature of the main method *must* be `public static void main` and it must take a `String...` or `String[]` as its single argument - those requirements can't be avoided. If you don't have `static` in the signature *then it isn't a main method*. Your real problem is the static context issue. – JonK Dec 01 '16 at 10:20
  • 1
    Did you search ? There is plainty of similar problem. It needs to be static. But after that, you need to know how to work with instance in a static context. – AxelH Dec 01 '16 at 10:20
  • 4
    Possible duplicate of [Non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – JonK Dec 01 '16 at 10:21

1 Answers1

2

In order to write proper java code, when you want to run a main method, you have to do as following:

In a Patient.java file:

public class Patient {
    private String name ;
    private int age ;
    public String newName;

    public String getName() {
        return name;
    }
    public void setName(){
        name = newName;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int newAge){
        age = newAge;
    }
}

In a PatientTest.java file:

public class PatientTest {

    public static boolean isChild(Patient patientToBeTested) {
        return (patientToBeTested.getAge() < 15);
    }

    public static void main(String[] args) {
        Patient patient = new Patient()
        patient.setAge(12) // add your age
        System.out.println(this.isChild(patient));
    }
}
DamCx
  • 1,047
  • 1
  • 11
  • 25