call functions in java code example

Example 1: calling method in java

public class MyClass {
 
    void myMethod() {
        System.out.println("You have called me! My name is: myMethod!");
    }
 
    public static void main(String[] args) {
        new MyClass().myMethod();
    }
}

Example 2: how to call a function from a class java

public class Classroom {
	 private String teacherName;
	
  	 public Classroom(String teacherName){
       this.teacherName = teacherName;
     }
  
  	 public String getTeacherName() {
        return teacherName;
     } 
}

public static void main(String[] args){
	Classroom firstGrade = new Classroom("John");
    /*Once you type the 'firstGrade.' your IDE most likely 
	will show all the functions it can acess*/
    firstGrade.getTeacherName();
}

Example 3: how to call a function in java

public class MyClass {
 
    static void myMethod() {
        System.out.println("You have called me! My name is: myMethod!");
    }
 
    public static void main(String[] args) {
        myMethod();
    }
}

Tags:

Java Example