-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaconstructor.java
More file actions
38 lines (32 loc) · 926 Bytes
/
Javaconstructor.java
File metadata and controls
38 lines (32 loc) · 926 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
package oops;
//it is a special method that is used to initialize objects
public class Javaconstructor {
int x; //class attribute
int y;
int modelyear;
String modelname;
//create class constructor for the Java constructor class
public Javaconstructor()
{
x=5; //set the initial value of class attribute x
}
//constructor parameters
public Javaconstructor(int z)
{
y=z;
}
public Javaconstructor(int year,String name)
{
modelyear=year;
modelname=name;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Javaconstructor myobj = new Javaconstructor();
System.out.println(myobj.x);
Javaconstructor myobj1=new Javaconstructor(10);
System.out.println(myobj1.y);
Javaconstructor myobj2 = new Javaconstructor(2012,"Toyota Fortuner");
System.out.println("The car "+myobj2.modelname+" released in "+myobj2.modelyear);
}
}