1A variable declared inside the class is called instance variable. Value of
2instance variable are instance specific.
3public class InstanceVariableDemo
4{
5 // instance variable declared inside the class and outside the method
6 int c;
7 public void subtract()
8 {
9 int x = 100;
10 int y = 50;
11 c = x - y;
12 System.out.println("Subtraction: " + c);
13 }
14 public void multiply()
15 {
16 int m = 10;
17 int n = 5;
18 c = m * n;
19 System.out.println("Multiplication: " + c);
20 }
21 public static void main(String[] args)
22 {
23 InstanceVariableDemo obj = new InstanceVariableDemo();
24 obj.subtract();
25 obj.multiply();
26 }
27}
1package Edureka;
2
3import java.util.Scanner;
4
5public class Student
6{
7
8public String name;
9
10private int marks;
11
12public Student (String stuName) {
13name = stuName;
14}
15public void setMarks(int stuMar) {
16marks = stuMar;
17}
18
19// This method prints the student details.
20public void printStu() {
21System.out.println("Name: " + name );
22System.out.println("Marks:" + marks);
23}
24
25public static void main(String args[]) {
26Student StuOne = new Student("Ross");
27Student StuTwo = new Student("Rachel");
28Student StuThree = new Student("Phoebe");
29
30StuOne.setMarks(98);
31StuTwo.setMarks(89);
32StuThree.setMarks(90);
33
34StuOne.printStu();
35StuTwo.printStu();
36StuThree.printStu();
37
38}
39}
40
1java decleartion
2
3int a, b, c; // Declares three ints, a, b, and c.
4int a = 10, b = 10; // Example of initialization
5byte B = 22; // initializes a byte type variable B.
6double pi = 3.14159; // declares and assigns a value of PI.
7char a = 'a'; // the char variable a iis initialized with value 'a'
1A variable declared inside the class
2 is called instance variable. Value of
3instance variable are instance specific.
4public class InstanceVariableDemo
5{
6 // instance variable declared inside the class and outside the method
7 int c;
8 public void subtract()
9 {
10 int x = 100;
11 int y = 50;
12 c = x - y;
13 System.out.println("Subtraction: " + c);
14 }
15 public void multiply()
16 {
17 int m = 10;
18 int n = 5;
19 c = m * n;
20 System.out.println("Multiplication: " + c);
21 }
22 public static void main(String[] args)
23 {
24 InstanceVariableDemo obj = new InstanceVariableDemo();
25 obj.subtract();
26 obj.multiply();
27 }
28}