Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java call method from another class example

public class method{
  public static void sayHello(){
    System.out.println("Hello World");
  }
}
public class app{
  public static void main(String[] args){
    method m = new method(); // Creating an instance from our class
    m.sayHello(); // using the class methods by the instance we created.
  }
}
Comment

how to access methods from another class in java

public class Alpha extends Beta{
     public void DoSomethingAlpha() {
          DoSomethingBeta();  //?
     }
}
Comment

how to call a void method from another class in java

1_ you should first create an instance from this class.
2_ once you create one, you use it for calling the class methods.
Comment

Calling A Class From Another Class In Java

class ClassComposition
{
    public static void main(String arg[])
    {
        Room hall = new Room("Hall"); // LINE A
        hall.area = 200.0;
        hall.paintColor = "Voilet";
        hall.flooring = "Marble";
        
        hall.ceilingFan = new Fan("Bajaj"); // LINE B
        hall.ceilingFan.speed = 500;     // LINE B1
        hall.ceilingFan.numberOfSpeeds = 5;
        
        hall.tube = new Light("Hall tube light"); // LINE C
        hall.tube.color = "White";
        hall.tube.watts = 40;
        hall.tube.tube = true;    // LINE C1
        
        Light bl = new Light("Yellow bed light"); // LINE D
        hall.bedLight = bl;
        hall.bedLight.color = "Yellow";
        hall.bedLight.watts = 50;
        hall.bedLight.tube = false;
        
        System.out.println("Hall fan speed : " + hall.ceilingFan.speed);
        System.out.println("Bed Light color : " + hall.bedLight.color);    
    }
}

class Fan
{
    String name;
    double speed;
    String color;
    int numberOfSpeeds;

    Fan(String name)
    {
        this.name = name;
    }
}

class Light
{
    String name;
    String color;
    int watts;
    boolean tube;

    Light(String name)
    {
        this.name = name;
    }
}

class Room
{
    String name;
    double area;
    String paintColor;
    String flooring;
    Fan ceilingFan;
    Light tube;
    Light bedLight;

    Room(String name)
    {
        this.name = name;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: write an infinite loop java 
Java :: how to get the max value of an array java 
Java :: java time difference 
Java :: Java Create ArrayList in Java 
Java :: primitive data types in java 
Java :: android kill other app programmatically by package 
Java :: java constructor chaining 
Java :: javac clear 
Java :: java base64 
Java :: sort list java 8 
Java :: wrapper classes in java 
Java :: string to int 
Java :: java integer division tofloat 
Java :: classcastexception in java 
Java :: switch case in android studio - java 
Java :: Java push() Method 
Java :: bukkit java get player count 
Java :: Java Writer Using FileWriter 
Java :: find elements in selenium 
Java :: initialize hashmap with values 
Java :: Android Bitmap to Base64 String 
Java :: java read a line of input 
Java :: hexstring to string In java 
Java :: install java using cmd 
Java :: Iterator to list (Java) 
Java :: enhanced for loop with arraylist 
Java :: how to make arraylist character 
Java :: H2 enabling remote database creation first 
Java :: in java how to compare two strings 
Java :: add a value to a list java in java hashmap 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =