TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Constructors Using Some Basic Examples in Java
Aayush Garg
Sep 28, 2019
5.2
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog you will learn Constructors Using Some Basic Examples in Java.
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
Next Recommended Reading
Copying The Values of One Object to Another Using Constructor in Java