Java Variables:
A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.
Variable is a name of memory location.
Types of Variables
There are three types of variables in Java:
1.local variable
2.instance variable
3.static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists.
*A local variable cannot be defined with "static" keyword.
***2) Instance Variable(Non static variable or object specific field)*
**
A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared among instances.
- Object specific variables
- Unique to each object
- Multiple Memory copies based on number of objects.
Eg:int amount = 80;
float weight = 78.2f;
3) Static variable(global variable)
A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory.
- Class specific variables
- Common for all objects
- Only One Memory Copy
Eg:static String cityName = โMadrasโ;
Reference :https://www.javatpoint.com/java-variables
Task 1
- Create a class called 'Jewellery'
- Create below static variables: static int goldPrice = 7100; static int silverPrice = 150; 2.1. Create below non-static variables int making_charges, wastage;
- Create main method.
- Inside main method, create instance as below Jewellery jewel1 = new Jewellery(); Jewellery jewel2 = new Jewellery();
- Using jewel1, assign making_charges and wastage.
- Using jewel2, assign making_charges and wastage.
- Call a non-static method as below. jewel1.bill(); jewel2.bill();
- Save, Compile and fix the error.
- Inside bill() method, print making_charges and wastage.
PROGRAM:
public class Jewellery
{
static int goldprice=7100;
static int silverprice=150;
int making_charge;
int wastage;
public static void main(String[] args)
{
Jewellery jewel1=new Jewellery();
Jewellery jewel2=new Jewellery();
jewel1.making_charge=1000;
jewel2.making_charge=200;
jewel1.wastage=300;
jewel2.wastage=150;
jewel1.bill1();
jewel2.bill2();
}
public void bill1()
{
System.out.println("jewel1"+goldprice+"\n"+making_charge+"\n"+wastage);
}
public void bill2()
{
System.out.println("jewel2"+silverprice+"\n"+making_charge+"\n"+wastage);
}
}
OUTPUT:
jewel1 7100
1000
300
jewel2 150
200
150
TASK 2:
- Create a class called 'Shop'
- Create below static variables: static int doorNo = 5; static int discount = 10; 2.1. Create below non-static variables int price, weight;
- Create main method.
- Inside main method, create instance as below Shop product1 = new Shop(); Shop product2 = new Shop();
- Using product1, assign price and weight.
- Using product2, assign price and weight.
- Call a non-static method as below. product1.bill(); product2.bill();
- Save, Compile and fix the error.
- Inside bill() method, print price and weight.
- Inside main method and inside bill() method, print doorNo and discount
PROGRAM:
public class Shop
{
static int doorNo = 5;
static int discount = 10;
int price;
float weight;
public static void main(String[] args)
{
Shop product1 = new Shop();
Shop product2 = new Shop();
product1.price=100;
product2.price=250;
product1.weight=5.5f;
product2.weight=3.4f;
System.out.println("door no"+"\n"+doorNo+"\n"+"discount"+"\n"+discount);
product1.bill1();
product2.bill2();
}
public void bill1()
{
System.out.println("product1"+"\n"+"price = "+price+"\n" +"weight = "+weight);
}
public void bill2()
{
System.out.println("product2"+"\n"+"price = "+price+"\n" +"weight = "+weight);
}
}
OUTPUT:
door no
5
discount
10
product1
price = 100
weight = 5.5
product2
price = 250
weight = 3.4
Top comments (0)