Skip to main content

Constructor-3


Constructor can be used for initializing instance variable of class.

Example1:-

class Student
{
private String name;
private int rollNo;
Student()
{
name="Rudraksha";
rollNo=101;
}
void disp()
{
System.out.println("Name is "+name);
System.out.println("RollNo is "+rollNo);
}
public static void main(String[]args)
{
Student s1=new Student();
s1.disp();
}
}

Output:-

C:\JAVATECH>javac Student.java
C:\JAVATECH>java Student

Name is Rudraksha
RollNo is 101


Example2:-

class Student
{
private String name;
private int rollNo;
Student(String n)
{
name=n;
}
Student(String n,int r)
{
name=n;
rollNo=r;
}
void disp()
{
System.out.println("Name is "+name);
System.out.println("RollNo is "+rollNo);
}

public static void main(String[]args)
{
Student s1=new Student("Rudraksha");
Student s2=new Student("Rudraksha",101);
s1.disp();
s2.disp();
}
}

Output:-

C:\JAVATECH>javac Student.java
C:\JAVATECH>java Student

Name is Rudraksha
RollNo is 0
Name is Rudraksha
RollNo is 101

Example3:-instance variable and local variable having same name

 class Student
{
private String name;
private int rollNo;

Student(String name,int rollNo)
{
name=name;
rollNo=rollNo;
}
void disp()
{
System.out.println("Name is "+name);
System.out.println("RollNo is "+rollNo);
}

public static void main(String[]args)
{
Student s1=new Student("Rudraksha",101);
s1.disp();

}
}

Output:-

C:\JAVATECH>javac Student.java
C:\JAVATECH>java Student

Name is null
RollNo is 0

Example4:-instance variable and local variable having same name than use this keyword for differentiate.

 class Student
{
private String name;
private int rollNo;

Student(String name,int rollNo)
{
this.name=name;
this.rollNo=rollNo;
}
void disp()
{
System.out.println("Name is "+name);
System.out.println("RollNo is "+rollNo);
}
public static void main(String[]args)
{
Student s1=new Student("Rudraksha",101);
s1.disp();

}
}

Output:-

C:\JAVATECH>javac Student.java
C:\JAVATECH>java Student

Name is Rudraksha
RollNo is 101

Here this keyword represent current object of class.



Comments

Popular posts from this blog

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Object,Data Hiding

Object:- Object is real time entity exist in the real world , the basic purpose of object is to transfer data from one location to another location.object is small memory , allocated inside primary memory(RAM).in java object created inside heap memory,actually in java RAM memory  is divided into some segment according to the type of variables for example. RAM==>Heap area+Stack area+String constant pool+class area  etc. when object created then inside object only non static class variables occupied memory and initialize with default values. non static class variables also known as instance variable reside into object in heap area,static variables occupied memory in class area at the time of class loading from HARD disk to RAM and all local variables occupied memory in stack area when reaching to variables. object  created from the class then what object contains is depend on class structure or class members.             ...