DAY-14 -Static method and Non static method..

Static method What is a static method in Java? Static methods are methods that are associated with a class rather than an object. They are declared using the keyword static. We can call static methods without creating an instance of the class. Static methods are often used to perform operations, not specific to any particular object, such as mathematical operations or checking the type of an object. Non static method A non-static method is an instance method and belongs to each object that is generated from the class Java Static Method and Non Static method Example: class Redbus { static String owner = "Hari"; int seat_no; //int float price; //float String bus_name; //String boolean seatavb; //boolean public static void Sleeper_bus() { System.out.println("Online booking"); } public void cast_bus() //non-static method definition { System.out.println("bus " +bus_name); } public static void main(String[] args) { Redbus bus1 = new Redbus(); Redbus bus2 = new Redbus(); Redbus bus3 = new Redbus(); bus1.bus_name = "lakshmi travels"; bus2.bus_name = "sundaram travels"; bus3.bus_name = "skp travels"; bus1.seat_no = 10; bus2.seat_no = 35; bus3.seat_no = 03; bus1.price = 2000.00f; bus2.price = 500.00f; bus3.price = 1500.00f; bus1.seatavb = true; bus2.seatavb = false; bus3.seatavb = true; System.out.println(bus1.bus_name); System.out.println(bus1.seat_no); System.out.println(bus1.price); System.out.println(bus1.seatavb); System.out.println(bus2.bus_name); System.out.println(bus2.seat_no); System.out.println(bus2.price); System.out.println(bus2.seatavb); System.out.println(bus3.bus_name); System.out.println(bus3.seat_no); System.out.println(bus3.price); System.out.println(bus3.seatavb); Redbus.Sleeper_bus(); bus1.cast_bus(); bus2.cast_bus(); bus3.cast_bus(); } }

Apr 5, 2025 - 02:53
 0
DAY-14 -Static method and Non static method..

Static method

What is a static method in Java?

Static methods are methods that are associated with a class rather than an object. They are declared using the keyword static.

We can call static methods without creating an instance of the class.

Static methods are often used to perform operations, not specific to any particular object, such as mathematical operations or checking the type of an object.

Non static method

A non-static method is an instance method and belongs to each object that is generated from the class

Java Static Method and Non Static method Example:

class Redbus
{
static String owner = "Hari";
int seat_no; //int
float price; //float
String bus_name; //String
boolean seatavb; //boolean

public static void Sleeper_bus()
{
System.out.println("Online booking");
}
public void cast_bus() //non-static method definition
{
System.out.println("bus " +bus_name);
}
public static void main(String[] args)
{
Redbus bus1 = new Redbus();
Redbus bus2 = new Redbus();
Redbus bus3 = new Redbus();
bus1.bus_name = "lakshmi travels";
bus2.bus_name = "sundaram travels";
bus3.bus_name = "skp travels";
bus1.seat_no = 10;
bus2.seat_no = 35;
bus3.seat_no = 03;
bus1.price = 2000.00f;
bus2.price = 500.00f;
bus3.price = 1500.00f;
bus1.seatavb = true;
bus2.seatavb = false;
bus3.seatavb = true;

System.out.println(bus1.bus_name);
System.out.println(bus1.seat_no);
System.out.println(bus1.price);
System.out.println(bus1.seatavb);
System.out.println(bus2.bus_name);
System.out.println(bus2.seat_no);
System.out.println(bus2.price);
System.out.println(bus2.seatavb);
System.out.println(bus3.bus_name);
System.out.println(bus3.seat_no);
System.out.println(bus3.price);
System.out.println(bus3.seatavb);
Redbus.Sleeper_bus();
bus1.cast_bus();
bus2.cast_bus();
bus3.cast_bus();

}
}