[AP CSA HW HELP] Static Methods

Static methods are methods that you can use without creating an instance or object of a class.

For example we have two files oopClass.java and main.java. We have a method inside of oopClass.java called staticDemo.

Here is the code of main.java and oopClass.java without static methods:

main.java:

public class main
{
public static void main(String[] args) {
oopClass obj = new oopClass();
obj.staticDemo();
}
}

oopClass.java:

public class oopClass {
public void staticDemo() {
System.out.println("Static methods are cool!");
}
}

Here is the code of main.java and oopClass.java with static methods:

main.java:

public class main
{
public static void main(String[] args) {
oopClass.staticDemo();
}
}

oopClass.java:

public class oopClass {
public static void staticDemo() {
System.out.println("Static methods are cool!");
}
}

Notice how we don't create a object of oopClass to access staticDemo method. Also notice how we have used the static keyword inside the declaration of staticDemo

The syntax of calling static methods is className.methodName();

Hope this post helped!

/r/APStudents Thread