DEV Community

Deva I
Deva I

Posted on

Java Program to Demonstrate Objects, Static and Non-static Variables and this Keyword

◾ In the previous blog our problem is the Building class is to store 3 different students information in a single object.

◾ So, using the below method to overcome this problem.

CODE :

public class SuperMarket
{
static String Shopname = "Food India";
String name;
int price;
public static void main(String[] args)
{
SuperMarket product1= new SuperMarket();
product1.name = "Rice";
product1.price = 1500;

SuperMarket product2= new SuperMarket();
product2.name = "Eggs";
product2.price = 100;

SuperMarket product3= new SuperMarket();
product3.name = "Ice cream";
product3.price = 150;
product1.buy();
product2.buy();
product3.buy();

}
public void buy(){
System.out.println("Shop Name : " + Shopname);
System.out.println("Product   : " + this.name);
System.out.println("Price : " + price);

}
}
Enter fullscreen mode Exit fullscreen mode

WORKING :

◾First create a class and I give the class name SuperMarket.

◾Then create static Variable, we need only one copy of this variable and all objects share the same Shopname Food India.

🔹Because all products are belongs to the same shop name.but the products have different names, So, use non-static variables for products names.

  • If all objects should share the same value => use a static variable.

  • If each object should have its own value => use an non-static variable.

🔹Every object should have its own values. so, create non-static variables like,

String name;
int price;
Enter fullscreen mode Exit fullscreen mode

🔹This tells every SuperMarket object have a name and a price.

🔹Then, when we create objects, java automatically allocates space for each objects.

◾Next create a objects what count you want.I create 3 objects product1,product2 and product3.Example in the above program I take product1 object.

SuperMarket product1= new SuperMarket();
Enter fullscreen mode Exit fullscreen mode

🔹The non-static variable allocate the space for each objects you create.

◾Then you assign your values,

product1.name = "Rice";
product1.price = 1500;

Enter fullscreen mode Exit fullscreen mode

◾The same process happens for product2 and product3

◾Then define the method to call the objects.

public void buy(){

}
Enter fullscreen mode Exit fullscreen mode
  • In javascript we use the function for event or actions,to define function give function keyword.

  • In java give a method name diretly buy().

  • In java we give return method, its not compulsary in javascript to give a return method.

  • But, in java must give void, if you not return anything.

◾Then call the buy() method on inside the main block.if you call the method on the anywhere of program without main method, java dosen,t know that.

🔹Because, JVM only runs on main method.

🔹If you give buy()inside the main, while call the buy() method when main method called.

  • You call the any methods on main method.

  • But, you won't define or create methods inside the main method or any method you create.

◾Then print the products name and price use this

  • this refers to current object.
 System.out.println(this.name);
Enter fullscreen mode Exit fullscreen mode

◾In this program price works without this.

System.out.println(price);
Enter fullscreen mode Exit fullscreen mode

🔹Java automatically assumes the above line like System.out.println(this.price);

  • Using this is optional because this working on implicit from backround.

  • In non-static method this works on internally without giving it.

  • But in static not using this keyword because, this keyword refers to object.

  • Using class name for static methods.

System.out.println("Shop Name : " + Shopname);
Enter fullscreen mode Exit fullscreen mode

🔹In this line Shopname is a static variable. but there is only one copy of Shopname, and it belongs to the class, not to any object.

🔹I give that line inside the buy() method.

public void buy() {
    System.out.println("Shop Name : " + Shopname);
}
Enter fullscreen mode Exit fullscreen mode

◾Java looks for Shopname and finds it in the class.

🔹When the buy() method is called it prints the Shopname.

🔹In every time prints the Shopname name when the each buy() method calls, in this code buy() method called by 3 times. so, it prints Shopname 3 times.

  • If you give this.Shopname java give a error message.because thisrefers to the current object.

OUTPUT :

Top comments (0)