In this article,Public Static Void main(String args[]) is explained in detail with examples.
Public
class Student
{
int rollNo;
String sName;
int age;
Student()
{
rollNo=1001;
sName="Sai";
age=21;
}
Student(int rollNo,String sName,int age)
{
this.rollNo=rollNo;
this.sName=sName;
this.age=age;
}
}
Student s1=new Student();
Student s2=new Student(1002,"RAVI",23);
b) 'This' Keyword is also used to call constructor of a class from another constructor.
Ex
this(x); //it calls constructor with 1 parameter.
this(x,y); //it calls contructor with 2 parameters.
Note
'this' Keyword is used by JVM as follows,
it represents current object under execution in memory.
Hence 'this' is separate for all the classes in a java program.
Ex
//Program related to Public Static Void main(String args[]).
class A
{
int x,y,z;
A(int x) //constructor with 1 parameter
{
this.x=x;
}
A(int x, int y)
{
this(x); //calling constructor with 1 parameter.
this.y=y;
}
A(int x, int y,in t z)
{
this(x,y);
this.z=z;
}
void display()
{
System.out.println("X: "+x);
System.out.println("Y: "+y);
System.out.println("Z: "+z);
}
}
class This
{
public static void main(String args[])
{
A obj1=new A(1);
obj1.display();
System.out.println();
A obj2=new A(10,20);
obj2.display();
System.out.println();
A obj3=new A(11,22,33);
obj3.dispaly();
}
}
Output
int x,y;
A(int x)
{
System.out.println("Hello");
this.x=x;
}
A(int x,int y)
{
this(x);
System.out.println("world");
this.y=y;
}
}
class B extends A
{
int x,y;
B(int i,int j,int k)
{
super(i);
System.out.println("Welcome.");
x=j;
y=k;
}
B(int i,int j,int k,int l)
{
super(i,j);
this.x=k;
this.y=l;
}
void display()
{
System.out.println("A X: "+super.x);
System.out.println("A Y: "+super.y);
System.out.println("B X: "+x);
System.out.println("B Y: "+y);
}
}
class Super3
{
public static void main(String args[])
{
B obj=new B(1,2,3);
obj.display();
System.out.println();
B obj1=new B(10,20,30,40);
obj1.display();
}
}
Output
Hi Friends, Please comment down your views in the comment section below. Thank you...
Public
Is an access modifier, it allows JVM to call main() outside the class from operating System command prompt.
Static
JVM calls main(), when a class is executed. It means without creating an object of our class JVM is invoking main() directly.Hence it is static.
Void
It indicates return type, the main method doesn't return any value back to JVM after execution.
main()
It is a user defined method with pre-defined declaration.It is the starting point of execution of a Java program. (From topic Public Static Void main(String args[]))
String args[]
main() is designed to take string array as arguments because JVM with the help of OS, calls main().In any OS, flow of information takes place in the form of text or string.Hence it is easy for operating system to pass string array as input arguments to main().
System.out.println()
System
It is a class available in java.lang package.This class represents your current computer system.
Out
It is a public static final variable(data-members) of print stream class as object.
"System.out" represents standard output device of yout computer system i.e, monitor
Println()
It is a method, available in printstream class,available in java.io package using with we can print some text or data on standard output device i.e,monitor.
void println();
|
|
void print(boolean);
|
void println(boolean);
|
void print(char);
|
void println(char);
|
void print(int);
|
void println(int);
|
void print(long);
|
void println(long);
|
void print(float);
|
void println(float);
|
void print(double);
|
void println(double);
|
void print(char[]);
|
void println(char[]);
|
void print(String);
|
void println(String);
|
void print(object);
|
void println(object);
|
In this first method "void print()" has 9 overloadings and second method "void println()" has 10 overloadings.
this keyword.
It is a special keyword, available to all Java Classes separately.This is provided by JVM
It is used only with in a class in two ways
a) It is used to refer data-members of a class from constructor and methods of that class provided if local variables have same-name as that of data members. (From topic Public Static Void main(String args[]))
Exclass Student
{
int rollNo;
String sName;
int age;
Student()
{
rollNo=1001;
sName="Sai";
age=21;
}
Student(int rollNo,String sName,int age)
{
this.rollNo=rollNo;
this.sName=sName;
this.age=age;
}
}
Student s1=new Student();
Student s2=new Student(1002,"RAVI",23);
b) 'This' Keyword is also used to call constructor of a class from another constructor.
Ex
this(x); //it calls constructor with 1 parameter.
this(x,y); //it calls contructor with 2 parameters.
Note
'this' Keyword is used by JVM as follows,
it represents current object under execution in memory.
Hence 'this' is separate for all the classes in a java program.
Ex
//Program related to Public Static Void main(String args[]).
class A
{
int x,y,z;
A(int x) //constructor with 1 parameter
{
this.x=x;
}
A(int x, int y)
{
this(x); //calling constructor with 1 parameter.
this.y=y;
}
A(int x, int y,in t z)
{
this(x,y);
this.z=z;
}
void display()
{
System.out.println("X: "+x);
System.out.println("Y: "+y);
System.out.println("Z: "+z);
}
}
class This
{
public static void main(String args[])
{
A obj1=new A(1);
obj1.display();
System.out.println();
A obj2=new A(10,20);
obj2.display();
System.out.println();
A obj3=new A(11,22,33);
obj3.dispaly();
}
}
Output
Super Keyword.
'Super' Keyword is used in inheritance i.e, only in a subclass.
It is used in three ways
i) To refer base-class data members from sub-class methods/constructors,provided base class data-members and sub class data members have the same name. (From topic Public Static Void main(String args[]))
Ex
Super.a=10;
Super.b=20;
ii) It is also used to refer base class constructors calling from sub class constructors.
Ex
Super(10);
Super(x,y);
iii) It is also used to call base class over ridden methods from sub class over ridding methods.
Ex
Super.method1();
Super.method2();
Ex
//Program related to Public Static Void main(String args[]).
//Program related to Public Static Void main(String args[]).
class A
{int x,y;
A(int x)
{
System.out.println("Hello");
this.x=x;
}
A(int x,int y)
{
this(x);
System.out.println("world");
this.y=y;
}
}
class B extends A
{
int x,y;
B(int i,int j,int k)
{
super(i);
System.out.println("Welcome.");
x=j;
y=k;
}
B(int i,int j,int k,int l)
{
super(i,j);
this.x=k;
this.y=l;
}
void display()
{
System.out.println("A X: "+super.x);
System.out.println("A Y: "+super.y);
System.out.println("B X: "+x);
System.out.println("B Y: "+y);
}
}
class Super3
{
public static void main(String args[])
{
B obj=new B(1,2,3);
obj.display();
System.out.println();
B obj1=new B(10,20,30,40);
obj1.display();
}
}
Output
Continue to next topic Nested classes in java.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment