1class Other{
2 public double num;
3 public Other(int num){
4 this.num = num;
5 System.out.println(num);
6 //print 5 to the console
7 }
8}
9
10class scratch{
11 public static void main(String[] args) {
12 Other method = new Other(5);
13 System.out.println(method.num);
14 //prints 5.0 to the console
15 }
16}
1When you use the keyword new for example
2JFrame j = new JFrame(); you are creating an instance of the class JFrame.
1// this keyword in java example
2import java.util.*;
3class Demo
4{
5 // instance variable
6 int m;
7 int n;
8 public void setValue(int m, int n)
9 {
10 m = m;
11 n = n;
12 }
13 public void showValue()
14 {
15 System.out.println("Value m = " + m);
16 System.out.println("Value n = " + n);
17 }
18}
19public class ThisKeywordDemo
20{
21 public static void main(String[] args)
22 {
23 Demo obj = new Demo();
24 obj.setValue(5, 6);
25 obj.showValue();
26 }
27}