I have a class let's say School in which I create an object Student. How would I call from Student class a method that is located in the School class without making it static?
Here's a quick example:
public class School(){
Student s1 = new Student();
public void createNewStudent(){
Student s2 = new Student();
}
}
public class Student(){
School.createNewStudent();
}
Thank you in advance!
Create an instance of School class within your Student class
ex:
School school = new School();
school.createNewStudent();
In java, class method are static method.
public class MyClass{
public static void myClassMethod() {
System.out.println("inside class method");
}
public void myInstanceMethod() {
MyClass.myClassMethod();
}
public static void main (String args[]) {
MyClass.myClassMethod();
MyClass myInstance = new MyClass();
myInstance.myInstanceMethod();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.