In this article,Constructor and Notations in Java are explained in detail with examples.
Like methods, Constructors can also take parameters, which acts like input values to the constructor.
Like methods, Constructors can also take parameters, which acts like input values to the constructor.
Based on this, constructors are divided into two types.
Default Constructor.
This is one search constructor, which does not take any single parameter.i.e, zero parameter constructor.
Ex
Student s1=new Student(); [()=zero parameter] (From topic Constructor and Notations in Java)
Parameterized Constructor.
This constructor takes atleast one parameter for creating an object.
Ex
Student s2=new Student(1002,"Ram",22);
[(1002,"Ram",22)=parameterized constructor]
Ex
class Student
{
int rollno;
String name;
int age;
Student() //Zero parameters in between brackets[()] it is called as default constructor.
{
rollno=1001;
name="Sai";
age=21;
}
Student(int r,String n,int a) //3 parameters between brackets[(r,n,a)].It is called as parameterized constructor.
{
rollno=r;
name=n;
age=a;
}
}
Ex
//program related to Constructor and Notations in Java--constructor class.
class Constructorclass
{
public static void main(String args[])
{
Exampleclass obj1=new Exampleclass(); //Default constructor.
Exampleclass obj2=new Exampleclass('b',20,200.8); //parameterized constructor.
System.out.println("object 1 char: "+obj1.cc);
System.out.println("object 1 char: "+obj1.ii);
System.out.println("object 1 char: "+obj1.dd);
System.out.println();
System.out.println("object 2 char: "+obj2.cc);
System.out.println("object 2 char: "+obj2.ii);
System.out.println("object 2 char: "+obj2.dd);
}
}
class Exampleclass
{
char cc;
int ii;
double dd;
Exampleclass() //default constructor.
{
cc='a';
ii=10;
dd=100.4;
}
Exampleclass(char c,int i,double d) //parameterized constrcutor.
{
cc=c;
ii=i;
dd=d;
}
}
Output
Notations in Java
They are simple notations for writing java programs.
1.Java class-Names these names should start with capital letters.
Ex Sample,Rectangle.
If it is a combination of two/more words then in each word, the first letter is only capital.
Ex ExampleClass,WrapperClass.
2.Data-Members of a class Should be in lower-case letters
Ex: int age;
string name;
float total;
If is is a combination of two/more words then from 2 nd word, the first letter is only capital.
Ex: int rollNumber;
string firstNumber;
int phoneNumber; (From topic Constructor and Notations in Java)
3.Methods of a class should be in lower -case letters.
Ex void dispaly(){--}
void accept(){--}
If it is a combination of two/more words then for 2nd word, the first letter is capital.
Ex void getStudentData(){--}
void displayStudentData(){--}
4.Local variables of a method should be in lower-case letters.
Ex public static void main(String args[])
{
int x,y;
float a,b;
int min,max;
}
If it is a combination of two/more then from 2nd word, only first letter is capital.
Ex public static void main(String args[])
{
int minVlue,maxValue;
}
5.Final variables, either data members or local variables,which act as constants in java should be in capital letters.
Ex final int MIN_AGE=18;
final int MAX_AGE=60;
final float PI=3.146;
6.Java package names are given in lowercase letters. (From topic Constructor and Notations in Java)
Ex java.lang
java.util
Continue to next topic Garbage Collection.
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.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment