Constructor and Keywords

Today we are going to learn about Constructor.

What is Constructor?

Constructor is used for Initializing instance specific Information in a block and it will named as Constructor because it will construct the entire class.

How is Constructor looks?

I.Constructor has a class name but it is not class because it doesn’t have the keyword class. Note – Constructor is given a class name to shows it important.

II. It looks like method but it is not a method because there is no void keyword and no return type.

Example:

public class Sslcmarkcalculator {

public Sslcmarkcalculator(int i, int j) {

    // TODO Auto-generated constructor stub
}

When the Constructor will be created?

Every time we create a Instance method Constructor will be automatically called.

Constructor will be called as Default and Parameterised constructor.

Default Constructor:

Every time we create a Instance method Constructor will be automatically called that is why we called it as Default Constructor.. But it is Invisible or hidden . Until we Pass any arguments to the Instance.

Parameterised constructor:

When we create a Instance method default constructor is already there but once we passed the inputs in the constructor it will became parameterised constructor and it is visible.

Depending upon the inputs we can create many constructor. From that we can understand constructor can be overloading.

Example:

public class Sslcmarkcalculator {

public Sslcmarkcalculator(int i, int j) {

    // TODO Auto-generated constructor stub
}

public Sslcmarkcalculator(int i, int j, String string) {

    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    Sslcmarkcalculator sslcobj=new Sslcmarkcalculator(90,30);
    int total=sslcobj.calculatemarks();
    total=sslcobj.calculategrade (total);
    Sslcmarkcalculator sslcobj1=new Sslcmarkcalculator(90,30,"age");

    // TODO Auto-generated method stub

}

Keyword Word- This:

This- keyword is used to indicate the Global level access for Instance variables .

Keyword – Static:

Static keyword is associated with the class level state and behavior . We can say static as common or stable (it can be changed).

Once we give static keyword in front a variable it is common for the entire class.

Example:

public class Bank {
static String Bankname= “BoB”;
static String Branchname=”Royapuram”;
String name,pancard ;
int age;

 public Bank(String name, int age, String pancard) {
     this.name=name;
     this.age=age;
     this.pancard=pancard;


 }

We can also create a Static Method.

What is public static void main(String[] args)?

public static void main(String[] args) is a starting point in java.

Keyword Public- Public is a access modifier this keyword decide who can access it and form where it can be accessed it will allow the jvm to call the method even from the other class .

we already discussed Static keyword it is used for declare a variable globally.

Void:

Void means empty or nothing . Without Void method can do activity but Return something .if void is removed error shows (Return type for the method is missing ) . Keyword void can be replaced by the datatype.

Example:

public static int main(String[] args) {
System.out.println(“Please tell me no.of Subjects”);
Scanner scannerobj= new Scanner(System.in);
int no1=scannerobj.nextInt();
int no2=scannerobj.nextInt();
CCalculator calc= new CCalculator();
int result=calc.add(98,99,89,98,99);
System.out.println(“result is”+result);
return result;

Main:

Main method is a starting point of the java program.

public class Bank{
public static void main(String args[]) {
System.out.println(“Hello world”);
}
}

Leave a comment

Design a site like this with WordPress.com
Get started