-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject2.java
More file actions
43 lines (39 loc) · 876 Bytes
/
Copy pathobject2.java
File metadata and controls
43 lines (39 loc) · 876 Bytes
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
import java.util.Scanner;
public class object2
{
static Scanner sc=new Scanner(System.in);
public static void main(String[] args)
{
String a;
myclass x= new myclass();
System.out.println("Enter a number:");
while(!sc.hasNextInt())
{
a=sc.next();
System.out.println("Please enter an integer value only!");
}
x.setI(sc.nextInt());
System.out.println("\nEntered value of i: " +(x.getI()));
}
}
class myclass
{
private int i,j;
public void setI(int i)
{
this.i=i;
}
{
i=1;
System.out.println("Hello! I am the initializer. When an obect is created, first I run & then my rival \'Constructor\'");
System.out.println("Value of i in the initializer: " +i);
}
public myclass()
{
System.out.println("\n\nHello! I am constructor to tell you that a new object is created!\n\n");
}
public int getI()
{
return (this.i);
}
}