-
Notifications
You must be signed in to change notification settings - Fork 0
/
this key word
52 lines (43 loc) · 1006 Bytes
/
this key word
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
This keyword use in java and it will return the current cunstructors
// Java Program to implement
// Java this reference
// Driver Class
public class Person {
// Fields Declared
String name;
int age;
// Constructor
Person(String name, int age)
{
this.name = name;
this.age = age;
}
// Getter for name
public String get_name() { return name; }
// Setter for name
public void change_name(String name)
{
this.name = name;
}
// Method to Print the Details of
// the person
public void printDetails()
{
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
System.out.println();
}
// main function
public static void main(String[] args)
{
// Objects Declared
Person first = new Person("ABC", 18);
Person second = new Person("XYZ", 22);
first.printDetails();
second.printDetails();
first.change_name("PQR");
System.out.println("Name has been changed to: "
+ first.get_name());
}
}
here is the code of java this kewords