[go: up one dir, main page]

0% found this document useful (0 votes)
12 views3 pages

Constructor Chaining

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Constructor Chaining

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Constructor Chaining have only single object known as singleton

Constructor chaining is a process class.


of calling one constructor from another In private constructor, only one object can
constructor in the same class. Since be created and the object is created within
constructor can only be called from the class and also all the methods are
another constructor, constructor chaining static. An object can not be created if a
is used for this purpose. private constructor is present inside a
To call constructor from another class. A class which have a private
constructor this keyword is used. This constructor and all the methods are static
keyword is used to refer current object. then it is called Utility class.
Constructor chaining is used when we Java program to create a private
want to perform multiple tasks by creating constructor
a single object of the class. class Test
class Test {
{ private Test ()
Test() {
{ System.out.println("This is a private
this(10); constructor.");
System.out.println("No Arguments }
Constructor"); public static void instanceMethod()
} {
Test(int x) Test obj = new Test();
{ }
this(15,25); }
System.out.println("x="+x); class Main
{
} public static void main(String[] args)
Test(int x, int y) {
{ Test.instanceMethod();
this(12.5,23.6,34.8); }
System.out.println("x="+x); }
System.out.println("y="+y);
} Java Singleton design using a private
Test(double x, double y, double z) constructor
{ class Test
System.out.println("x="+x); {
System.out.println("y="+y); private static Test t;
System.out.println("z="+z); private Test()
} {
public static void main(String arg[]) System.out.println("Inside Private
{ Constructor");
Test object = new Test(); }
} public static Test getInstance()
} {
Private Constructors if(t == null) {
t = new Test();
In Java, we can create private constructor }
to prevent class being instantiate. It return t;
means by declaring a private constructor, it }
restricts to create object of that class.
Private constructors are used to public void display() {
create singleton class. A class which can
System.out.println("Singleton Pattern
is achieved"); public void show()
} {
} System.out.println(eid + "-" + name +
"-" + company);
class Main { }
public static void main(String[] args) {
Test t1; public static void main( String[] args )
{
// call the getInstance method Employee se1 = new Employee();
t1= Test.getInstance(); se1.eid = 104;
t1.display(); se1.name = "Abhijit";
} se1.show();
}
Static is a keyword in Java which is used Employee se2 = new Employee();
to declare static stuffs. It can be used to se2.eid = 108;
create variable, method block or a class. se2.name = "ankit";
Static variable or method can be accessed se2.show();
without instance of a class because it }
belongs to class. Static members are
common for all the instances of the class }
but non-static members are different for
each instance of the class Static variable vs Instance variable

Static Variables

Static variable Instance Variable


Static variables defined as a class member
can be accessed without object of that
class. Static variable is initialized
once and shared among different Represent common Represent unique
objects of the class. All the object of the property property
class having static variable will have the
same instance of static variable.
Note: Static variable is used to represent
common property of a class. It saves Accessed using class
memory. name (can be accessed Accessed using
using object name as object
Example: well)
Suppose there are 100 employee in a
company. All employee have its unique
name and employee id but company name
will be same all 100 employee. Here Allocated new
company name is the common property. Allocated memory only memory each time
So if you create a class to store employee once a new object is
detail, then declare company_name field as created
static

class Employee
public class Test
{
{
int eid;
static int x = 100;
String name;
int y = 100;
static String company = "Studytonight";
public void increment()
{ {
x++; y++; int eid;
} String name;
public static void main( String[] args ) static String company_name;
{
Test t1 = new Test(); static {
Test t2 = new Test(); company_name ="StudyTonight";
t1.increment(); //static block invoked before main()
t2.increment(); method
System.out.println(t2.y); }
System.out.println(Test.x);
//accessed without any instance of class. public void show()
} {
} System.out.println(eid+" "+name+"
"+company_name);
Static Method in Java }
public static void main( String[] args )
{
A method can also be declared as static. ST_Employee se1 = new
Static methods do not need instance of its ST_Employee();
class for being accessed. main() method is se1.eid = 104;
the most common example of static se1.name = "Abhijit";
method. main() method is declared as se1.show();
static because it is called before any object }
of the class is created.
}
class Test
{

public static void square(int x)


{
System.out.println(x*x);
}

public static void main (String[] arg)


{
square(8) //static method square () is
called without any instance of class.
}
}

Static block in Java

Static block is used to initialize static data


members. Static block executes even
before main() method.
It executes when the class is loaded in the
memory. A class can have multiple Static
blocks, which will execute in the same
sequence in which they are programmed.

class ST_Employee

You might also like