You learned from the Java Methods chapter that methods are declared within a class, and that they are used to perform certain actions:
Example
Create a method named myMethod[]
in Main:
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
}
myMethod[]
prints a text [the action], when it is called. To call a method, write the method's name followed by two parentheses [] and a semicolon;
Example
Inside main
, call myMethod[]
:
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
Try it Yourself »
Static vs. Public
You will often see Java programs that have either
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
0 or public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
1 attributes and methods.In the example above, we created a
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
0 method, which means that it can be accessed without creating an object of the class, unlike public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
1, which can only be accessed by objects:Example
An example to demonstrate the differences between
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
0 and public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
1 methods:public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
Try it Yourself »
Note: You will learn more about these keywords [called modifiers] in the Java Modifiers chapter.
Access Methods With an Object
Example
Create a Car object named
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
6. Call the public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
7 and public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
8 methods on the public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
6 object, and run the program:// Create a Main class
public class Main {
// Create a fullThrottle[] method
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
// Create a speed[] method and add a parameter
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
// Inside main, call the methods on the myCar object
public static void main[String[] args] {
Main myCar = new Main[]; // Create a myCar object
myCar.fullThrottle[]; // Call the fullThrottle[] method
myCar.speed[200]; // Call the speed[] method
}
}
// The car is going as fast as it can!
// Max speed is: 200
Try it Yourself »
Example explained
1] We created a custom
public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
0 class with the public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
1 keyword.2] We created the
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
7 and public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
8 methods in the public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
0 class.3] The
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
7 method and the public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
8 method will print out some text, when they are called.4] The
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
8 method accepts an public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
8 parameter called public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
9 - we will use this in 8].5] In order to use the
public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
0 class and its methods, we need to create an object of the public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
0 Class.6] Then, go to the
// Create a Main class
public class Main {
// Create a fullThrottle[] method
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
// Create a speed[] method and add a parameter
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
// Inside main, call the methods on the myCar object
public static void main[String[] args] {
Main myCar = new Main[]; // Create a myCar object
myCar.fullThrottle[]; // Call the fullThrottle[] method
myCar.speed[200]; // Call the speed[] method
}
}
// The car is going as fast as it can!
// Max speed is: 200
2 method, which you know by now is a built-in Java method that runs your program [any code inside main is executed].7] By using the
// Create a Main class
public class Main {
// Create a fullThrottle[] method
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
// Create a speed[] method and add a parameter
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
// Inside main, call the methods on the myCar object
public static void main[String[] args] {
Main myCar = new Main[]; // Create a myCar object
myCar.fullThrottle[]; // Call the fullThrottle[] method
myCar.speed[200]; // Call the speed[] method
}
}
// The car is going as fast as it can!
// Max speed is: 200
3 keyword we created an object with the name public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
6.8] Then, we call the
public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
7 and public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
8 methods on the public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
6 object, and run the program using the name of the object [public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
6], followed by a dot [// Create a Main class
public class Main {
// Create a fullThrottle[] method
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
// Create a speed[] method and add a parameter
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
// Inside main, call the methods on the myCar object
public static void main[String[] args] {
Main myCar = new Main[]; // Create a myCar object
myCar.fullThrottle[]; // Call the fullThrottle[] method
myCar.speed[200]; // Call the speed[] method
}
}
// The car is going as fast as it can!
// Max speed is: 200
9], followed by the name of the method [public class Main {
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
}
0 and public class Main {
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
}
1]. Notice that we add an public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
8 parameter of 200 inside the public class Main {
static void myMethod[] {
System.out.println["Hello World!"];
}
public static void main[String[] args] {
myMethod[];
}
}
// Outputs "Hello World!"
8 method.Remember that..
The dot [
// Create a Main class
public class Main {
// Create a fullThrottle[] method
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
// Create a speed[] method and add a parameter
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
// Inside main, call the methods on the myCar object
public static void main[String[] args] {
Main myCar = new Main[]; // Create a myCar object
myCar.fullThrottle[]; // Call the fullThrottle[] method
myCar.speed[200]; // Call the speed[] method
}
}
// The car is going as fast as it can!
// Max speed is: 200
9] is used to access the object's attributes and methods.To call a method in Java, write the method name followed by a set of parentheses [], followed by a semicolon [
public class Main {
public void fullThrottle[] {
System.out.println["The car is going as fast as it can!"];
}
public void speed[int maxSpeed] {
System.out.println["Max speed is: " + maxSpeed];
}
}
5].A class must have a matching filename [
public class Main {
// Static method
static void myStaticMethod[] {
System.out.println["Static methods can be called without creating objects"];
}
// Public method
public void myPublicMethod[] {
System.out.println["Public methods must be called by creating objects"];
}
// Main method
public static void main[String[] args] {
myStaticMethod[]; // Call the static method
// myPublicMethod[]; This would compile an error
Main myObj = new Main[]; // Create an object of Main
myObj.myPublicMethod[]; // Call the public method on the object
}
}
0 and Main.java].Using Multiple Classes
Like we specified in the Classes chapter, it is a good practice to create an object of a class and access it in another class.
Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory: