◾ 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);
}
}
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;
🔹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();
🔹The non-static variable allocate the space for each objects you create.
◾Then you assign your values,
product1.name = "Rice";
product1.price = 1500;
◾The same process happens for product2 and product3
◾Then define the method to call the objects.
public void buy(){
}
In javascript we use the function for event or actions,to define function give
functionkeyword.In java give a method name diretly
buy().In java we give return method, its not compulsary in javascript to give a
returnmethod.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
mainmethod.But, you won't define or create methods inside the
mainmethod or any method you create.
◾Then print the products name and price use this
-
thisrefers to current object.
System.out.println(this.name);
◾In this program price works without this.
System.out.println(price);
🔹Java automatically assumes the above line like System.out.println(this.price);
Using
thisis optional becausethisworking on implicit from backround.In non-static method
thisworks on internally without giving it.But in static not using
thiskeyword because,thiskeyword refers to object.Using class name for static methods.
System.out.println("Shop Name : " + Shopname);
🔹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);
}
◾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.Shopnamejava give a error message.becausethisrefers to the current object.
OUTPUT :

Top comments (0)