문제

Really need to know how to call this method in another class thnx

public static void printClass(){
    System.out.println(subjectName + " Class List");

    for(Student i : studentList)
    {   
        Student.printStudent();
    }
}
도움이 되었습니까?

해결책

Since it's a static method (class method), you can call it in two ways:

NameOfClass.printClass();

or

NameOfClass someInstance = new NameOfClass(...); // create instance
someInstance.printClass();

Why? Because class methods doesn't need an instance of the class to be called, so you can call it with only using the name of the class (first way). But Java also provides the feature to call the static method using an instance of the class.

다른 팁

You can call a static method like this:

ClassName.methodName();

So, e.g., if you're class is called SomeClass:

SomeClass.printClass();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top