Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Constructors Using Some Basic Examples in Java
WhatsApp
Aayush Garg
5y
5.4
k
0
1
25
Blog
Constructors Using Some Basic Examples:
Constructors are the special methods whose name is the same as the class name.
Constructors do not have return types even void also.
Constructors will be invoked by the JVM automatically at the time of object creation.
Constructors are mainly used to initialize instance variables of class with a different set of values.
Examples:
Lab1.java
class
Lab1 {
public
static
void
main(String args[]) {
Student stu1 =
new
Student();
stu1.show();
Student stu2 =
new
Student();
stu2.show();
}
}
class
Student {
intsid;
String sname;
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab2.java
class
Lab2 {
public
static
void
main(String args[]) {
Student stu =
new
Student();
stu.sid =
99
;
stu.sname =
"Ayush"
;
stu.show();
}
}
class
Student {
intsid;
String sname;
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab3.java
class
Lab3 {
public
static
void
main(String args[]) {
Student stu =
new
Student();
stu.sid =
99
;
stu.sname =
"Ayush"
;
stu.show();
}
}
class
Student {
intsid;
String sname;
Student() {
System.out.println(
"Student Default Constructor"
);
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab4.java
class
Lab4 {
public
static
void
main(String args[]) {
Student stu =
new
Student();
stu.sid =
99
;
stu.sname =
"Ayush"
;
stu.show();
}
}
class
Student {
intsid;
String sname;
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab5.java
class
Lab5 {
public
static
void
main(String args[]) {
Student stu1 =
new
Student(
88
,
"Ayush"
);
stu1.show();
Student stu2 =
new
Student(
99
,
"Garg"
);
stu2.show();
}
}
class
Student {
intsid;
String sname;
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab6.java
class
Lab6 {
public
static
void
main(String args[]) {
Student stu1 =
new
Student();
stu1.show();
Student stu2 =
new
Student(
99
,
"Garg"
);
stu2.show();
}
}
class
Student {
intsid;
String sname;
Student() {
System.out.println(
"Student Default Constructor"
);
}
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab7.java
class
Lab7 {
public
static
void
main(String args[]) {
Student stu1 =
new
Student(
99
,
"Ayu"
,
"
[email protected]
"
,
9999
);
stu1.show();
Student stu2 =
new
Student(
88
,
"sh"
,
"
[email protected]
"
);
stu2.show();
Student stu3 =
new
Student(
77
,
"AG"
);
stu3.show();
Student stu4 =
new
Student();
stu4.show();
}
}
class
Student {
intsid;
String sname;
String email;
long
phone;
Student(
int
id, Stringsn, Stringem, longph) {
System.out.println(
"Student 4-Arg Constructor"
);
sid = id;
sname = sn;
email = em;
phone = ph;
}
Student(
int
id, Stringsn, Stringem) {
System.out.println(
"Student 3-Arg Constructor"
);
sid = id;
email = em;
sname = sn;
}
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
Student() {
System.out.println(
"Student Default constructor"
);
}
void
show() {
System.out.println(sid +
"\t"
+ sname +
"\t"
+
"email"
+
"phone"
);
}
}
Lab8.java
class
Lab8 {
public
static
void
main(String args[]) {
Student stu3 =
new
Student(
77
,
"AG"
);
stu3.show();
}
}
class
Student {
intsid;
String sname;
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
Student(
int
x,
int
y) {
System.out.println(
"Student 2-Arg constructor"
);
sid = x;
sname = y;
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab9.java
class
Lab9 {
public
static
void
main(String args[]) {
Student stu3 =
new
Student(
77
,
"AG"
);
stu3.show();
}
}
class
Student {
intsid;
String sname;
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
Student(String y,
int
x) {
System.out.println(
"Student 2-Arg constructor"
);
sname = y;
sid = x;
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab10.java
class
Lab10 {
public
static
void
main(String args[]) {
Student stu =
new
Student();
stu.Student(
77
,
"AG"
);
stu.show();
}
}
class
Student {
intsid;
String sname;
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
Student() {
System.out.println(
"Student default constructor"
);
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab11.java
class
Lab11 {
public
static
void
main(String args[]) {
Student stu =
new
Student();
stu.Student(
77
,
"AG"
);
stu.show();
}
}
class
Student {
intsid;
String sname;
void
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
Student() {
System.out.println(
"Student default constructor"
);
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab12.java
class
Lab12 {
public
static
void
main(String args[]) {
Student stu =
new
Student();
stu.Student(
77
,
"AG"
);
stu.show();
}
}
class
Student {
intsid;
String sname;
void
Student(
int
id, Stringsn) {
System.out.println(
"Student 2-Arg Constructor"
);
sid = id;
sname = sn;
}
void
show() {
System.out.println(sid +
"\t"
+ sname);
}
}
Lab13.java
class
Lab13 {
public
static
void
main(String args[]) {
Student stu =
new
Student(-
12
);
stu.show();
}
}
class
Student {
int
age =
18
;
Student(
int
ag) {
System.out.println(
"Student 2-Arg Constructor"
);
age = ag;
}
void
show() {
System.out.println(age);
}
}
Lab14.java
class
Lab14 {
public
static
void
main(String args[]) {
Student stu =
new
Student(-
12
);
stu.show();
}
}
class
Student {
int
age =
18
;
Student(
int
ag) {
System.out.println(
"Student 2-Arg Constructor"
);
if
(ag =
18
)
return
;
age = ag;
}
void
show() {
System.out.println(age);
}
}
Lab15.java
class
Lab15 {
public
static
void
main(String args[]) {
Student stu =
new
Student(-
12
);
stu.show();
}
}
class
Student {
int
age =
18
;
Student(
int
ag) {
System.out.println(
"Student 2-Arg Constructor"
);
if
(ag =
18
)
return
0
;
age = ag;
}
void
show() {
System.out.println(age);
}
}
Constructors Using Some Basic Examples in Java
People also reading
Membership not found