DEV Community

Neelakandan R
Neelakandan R

Posted on

Today class--28/12/2024 Variables ,TASK1 ,TASK 2

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
Enter fullscreen mode Exit fullscreen mode

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

  1. Create a class called 'Jewellery'
  2. Create below static variables: static int goldPrice = 7100; static int silverPrice = 150; 2.1. Create below non-static variables int making_charges, wastage;
  3. Create main method.
  4. Inside main method, create instance as below Jewellery jewel1 = new Jewellery(); Jewellery jewel2 = new Jewellery();
  5. Using jewel1, assign making_charges and wastage.
  6. Using jewel2, assign making_charges and wastage.
  7. Call a non-static method as below. jewel1.bill(); jewel2.bill();
  8. Save, Compile and fix the error.
  9. 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);
}
}


Enter fullscreen mode Exit fullscreen mode

OUTPUT:


jewel1 7100
1000
300
jewel2 150
200
150

Enter fullscreen mode Exit fullscreen mode

TASK 2:

  1. Create a class called 'Shop'
  2. Create below static variables: static int doorNo = 5; static int discount = 10; 2.1. Create below non-static variables int price, weight;
  3. Create main method.
  4. Inside main method, create instance as below Shop product1 = new Shop(); Shop product2 = new Shop();
  5. Using product1, assign price and weight.
  6. Using product2, assign price and weight.
  7. Call a non-static method as below. product1.bill(); product2.bill();
  8. Save, Compile and fix the error.
  9. Inside bill() method, print price and weight.
  10. 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);
}
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

door no
5
discount
10
product1
price = 100
weight = 5.5
product2
price = 250
weight = 3.4
Enter fullscreen mode Exit fullscreen mode

Top comments (0)