Constructors
1.
A java constructor has the same name as the name of the class to which it
belongs. Constructor’s syntax does not include a return type, since
constructors never return a value.
2.
Constructors may include parameters of various types. When the constructor is
invoked using the new operator, the types must match those that are specified
in the constructor definition.
3.
Java provides a default constructor which takes no arguments and performs no
special actions or initializations, when no explicit constructors are provided.
Constructors
are of two types: ---
1. Default constructors
It
doesn't have any user-defined logic and it consists of only one statement
super();
We
can find the default cons in our program by using javap command.
--javap
is used to decompile the program.
Ex:--
class ConsEx
{
ConsEx()
{
super();
}
p.s.v.main(String
[]args)
{
ConsEx obj= new ConsEx();
s.o.p(obj);
}
}
Note: if we dont
provide the cons then only compiler provides default cons.
2.userdefined
constructor
ex:---
class userDefine
{
int a,b;
userDefine()
{
a=3;
b=4;
System.out.println("userdefined
constructor");
}
public static void
main(String [] args)
{
userDefine us = new
userDefine();
System.out.println(us.a);
System.out.println(us.b);
}
}
We can write any no of
user-defined cons with in a prog with different no of paramrters
Ex:--
class
userDefineParamCons
{
int a,b;
userDefineParamCons()
{
a=3;
b=4;
System.out.println(“a=”+a+”\n”+”b=”+b);
System.out.println("userdefined
constructor");
}
userDefineParamCons(int
s)
{
a=s;
b=s;
System.out.println(a
+”\n”+b);
System.out.println("one
param constructor");
}
userDefineParamCons(int
s,int d)
{
a=s;
b=d;
System.out.println(a);
System.out.println(b);
System.out.println("two
param constructor");
}
userDefineParamCons(String
s,int d,int f)
{
System.out.println("three
param constructor");
System.out.println(s);
System.out.println(d);
System.out.println(f);
}
public static void
main(String [] args)
{
userDefineParamCons us = new userDefineParamCons();
//System.out.println(us.a);
//System.out.println(us.b);
userDefineParamCons us1 = new userDefineParamCons(2);
//System.out.println(us1.a);
//System.out.println(us1.b);
userDefineParamCons us2 = new userDefineParamCons(12,13);
//System.out.println("a="+us2.a+"
\n" +"b="+us2.b);
new
userDefineParamCons("rama",14,15);
}
}
Java
Overloaded Constructors
Like
methods, constructors can also be overloaded. Since the constructors in a class
all have the same name as the class, their signatures are differentiated by
their parameter lists. The above example shows that the Cube1constructor is
overloaded one being the default constructor and the other being a parameterized
constructor.
Constructor
overloading:--
We can overload the
constructor also means having same name with different no of parameters class cons123
{
int d=5;
cons123()
{
System.out.println("no=param-constructor");
}
cons123(int x)
{
System.out.println("one-param-constructor"
+ x);
}
cons123(int x,int y)
{
System.out.println(x+y);
System.out.println("two-param-constructor"
);
}
cons123(double d,int x)
{
System.out.println("constructor"
+ (d+x));
}
void disp()
{
System.out.println(this.d);
}
public static void
main(String [] args)
{
new cons123();
new cons123(2);
new cons123(3,4);
new cons123(2.2,5);
cons123 obj=new cons123();
obj.disp();
}
}
Ex:
---
In
this example we will see that how to to implement the constructor feature in a
class. This program is using two classes. First class is another and second is
the main class which name is constructing. In the Construct class two objects
(a and b) are created by using the overloaded another Constructor by passing
different arguments and calculated the are of the different rectangle by
passing different values for the constructor.
class another{
int x,y;
another(int a, int b){
x = a;
y = b;
}
another()
int x,y;
another(int a, int b){
x = a;
y = b;
}
another()
{
}
int area(){
int ar = x*y;
return(ar);
}
}
public class Construct{
public static void main(String[] args)
{
another b = new another();
}
int area(){
int ar = x*y;
return(ar);
}
}
public class Construct{
public static void main(String[] args)
{
another b = new another();
b.x = 2;
b.y = 3;
System.out.println("Area of rectangle : " + b.area());
System.out.println("Value of y in another class : " + b.y);
another a = new another(1,1);
System.out.println("Area of rectangle : " + a.area());
System.out.println("Value of x in another class : " + a.x);
}
}
b.y = 3;
System.out.println("Area of rectangle : " + b.area());
System.out.println("Value of y in another class : " + b.y);
another a = new another(1,1);
System.out.println("Area of rectangle : " + a.area());
System.out.println("Value of x in another class : " + a.x);
}
}
It
is possible to use this (), to implement local chaining of constructors in a
class. The this() call in a constructorinvokes the an other constructor with
the corresponding parameter list within the same class.
/*constructor
chaining
Means calling another
constructor through one constuctor
Here first it calls
this(10,10)(this---means class cons)Then
it has two args then calls second cons then it calls third cons etc.*/
class AB
{
AB()
{
this(1);
System.out.println("no
param");
}
AB(int n)
{
this(1,2);
System.out.println("one
param");
}
AB(int a,int b)
{
System.out.println("two params");
}
public static void main(String[] args)
{
AB a =
new AB();
//System.out.println("Hello World!");
}
Note: “this” is a non-static reference variable, which
consists of current class object reference.
Note: “super ” is a
non-static reference variable, which consists of super class object reference.
Note: “ this()” used to
call current class constructor
Note: “ super()” used
to call super class constructor
Static & Non-Static Members
MAIN
Static & Non-Static Members
Static members :
If any member declaration consists of “static ” keyword which is known as static member.
These are automatically loaded into JVM at class-loading-time. To access a static members, no instance needs to be created.those Static members are: ---
1. Static Variables
2. Static Methods
3. Static Blocks.
1. Static Variable
Syntax: ---<Accesmodifier> static <datatype> <varname>;
Ex: --- private static int ab;
1. Features of Static Variable: ---
ü Automatically loded into JVM.
ü Declared in Class level only.
ü Can be accesses directly by its name anywhere in the program.
ü Can also access with class name as well as with Object also.
Ex:--
//Static Variables
Ex:--
class StaticVar {
static int x;
public static void main (String [] args)
{
//static int z; error can’t give static decla in method level
StaticVar obj=new StaticVar ();
System.out.println(x);
//System.out.println (obj.x);
//System.out.println (StaticVar.x);
}
}
2. About Static methods: ---
If any method decla contains Static keyword we call it as Static method.
Features of Static Variable: ---
1. Automatically loded into JVM.
2. Can be accesses directly by its method name anywhere in the program.
3. We can also call static method by using object as well as by using its class name.
Syntax: ---
static void methodname (){
------------
}
Ex:---
class staticvar1
{
static int x;
static int sum(int x,int y)
{
return (x+y);
}
public static void main(String [] args)
{
staticvar1 obj=new staticvar1();
System.out.println(obj.sum(2,4));
System.out.println(sum(2,4));
System.out.println(staticvar1.sum(2,4));
int d=sum(2,4);
System.out.println("hello" +d);
}
}
3. Static Blocks: ---
Static Blocks are executed automatically by JVM ,no need of calling.
Syntax:--
static {
----------------
}
Note:- Static blocks are used to write the common logic which is executed at the time of class loading time.
Ex:---
public class StaticDemo {
static {
show();
}
public StaticDemo() {
System.out.println("CONSTRUCTOR");
}
public static void show() {
System.out.println("MESSAGE");
}
public static void main(String[] args) {
System.out.println("MAIN");
StaticDemo s = new StaticDemo();
StaticDemo s2 = new StaticDemo();
}
static
{
System.out.println("STATIC BLOCK");
System.out.println("StaticDemo.name = " + name);
}
}
You'll see following output:
MESSAGE
STATIC BLOCK
StaticDemo.name = NAME
HELLO
CONSTRUCTOR
HELLO
CONSTRUCTOR
*/
Non-Static Members
ü If any member decal doesn’t consist of “static” keyword we call it as Non-Static-member.
ü These are not directly loaded into JVM.
ü We can call these members through Object only. Those are..
1. Non-Static Variables
2. Non-Static Methods
3. Non-Static Blocks.
1. Non-Static Variable
Syntax: ---<Accesmodifier> <datatype> <varname>;
Ex: --- private int ab;
Ex:--
class NonStaticVar {
int x;
public static void main (String [] args) {
NonStaticVar obj=new NonStaticVar ();
System.out.println(obj.x);
} }
2. About Non-Static methods: ---If any method declaration dose’t contains static keyword we call it as Non-Static method.
Syntax: ---
void methodname (){
------------
}
Ex:---
class NSM
{
int x;
int area(int x,int y)
{
return (x*y);
}
public static void main(String [] args)
{
NSM n=new NSM();
int d=n.area(2,4);
System.out.println(" area is : " +d);
}
}
3. Non-Static Blocks: ---
Non-Static Blocks are executed automatically by JVM ,no need of calling.
Syntax:--
{
Statements …
}
Note:- Non-Static blocks are used to write the common logic which is executed at the time of each object creation time.
Class NSB{
{
System.out.println(“am a non-static block ”);
}
p s v m(String ar[]){
NSB obj=new NSB();
}
}
Output:
Am a non-static block.
No comments:
Post a Comment