final keyword, new keyboard, this keyword and static keyword
Skip to main content

final keyword, new keyboard, this keyword and static keyword

 default keyword in java, final keyword in java, java keywords, java keywords list, java keywords with examples, final keyword in java with example, static keyword in java, static and final keyword in java, new keyword in java, new keyword in javascript, this keyword in java, this keyword in javascript




final keyword, new keyboard, this keyword and static keyword :-

         This tutorial is explain about Java Keywords  likes final keyword, new keyword, static keyword and this keyword with explanation and given example of Java Programming.


Java Keywords in Java:-

1. Final Keyword
2. New Keyword
3. This Keyword
4. Static Keyword

 Final Keyword:

        Final keyword is a non access modifier used for classattributes and method. If you use final keyword then that value is not changeable. Final keyword is use before of method then method does not override and use before a class then class is not inherited. Final keyword you used when you require that variable always store same value

Declaration Example :- 

final int mValue=5;

mValue=7; // its generated error 

System.out.println(+mValue);


Static Keyword:

         Static is a keyword in java. which is mainly used for a memory allocation. The static keyword is used to share same variable and method for given class. For example in company there have many employees working there ID and Name is different but company name is same then you use static keyword for all the employees.


Most Read:- Inheritance in Java?


class Company{

          int mId;

String mEname;

static String mCompanyname="GK InfoTech";

Company( int Id, String name){

      mId=Id;

      mEname=name;

}

display(){

System.out.println("EmployeeId="+mId,\n "Employee Name="+mEname, \n "Company Name="+mCompanyname);

}

}

class EmployeeMain {

public static void main(String [] args){

Company mCompany1= new Company(1, "Raj");

Company mCompany2= new Company(2, "Rakesh");

mCompany1.display();

mCompany2.display();

}

}

Output:-

EmployeeId= 1 

Employee Name= Raj

Company Name= GK InfoTech

EmployeeId= 2

Employee Name= Rakesh

Company Name= GK InfoTech


Most Read:-what is abstraction and used abstract class & method in Java?


new Keyword:-

        new keyword is used to create an instance of the class. It is used to allocate a memory for a new objects and returning a reference of that memory.

Test obj = new Test();


this Keyword:-

        this keyword is a reference variable that refer to the objects. this can be used to invoke in class, method and constructor.


Problem without use of this Keyword in Class:-

class Company{

          int mId;

String mEname;

static String mCompanyname="GK InfoTech";

Company( int mId, String mEName){

      mId=Id;

      mEname=name;

}

display(){

System.out.println("EmployeeId="+mId,\n "Employee Name="+mEname, \n "Company Name="+mCompanyname);

}

}

class EmployeeMain {

public static void main(String [] args){

Company mCompany1= new Company(1, "Raj");

Company mCompany2= new Company(2, "Rakesh");

mCompany1.display();

mCompany2.display();

}

}

Output:-

EmployeeId= 1 

Employee Name=  null

Company Name= GK InfoTech

EmployeeId= 2

Employee Name= null

Company Name= GK InfoTech


Most Read:- Defination of looping and types use in Java


Using this Keyword:-

class Company{

          int mId;

String mEname;

static String mCompanyname="GK InfoTech";

Company( int mId, String mEName){

      this.mId=Id;

      this.mEname=name;

}

display(){

System.out.println("EmployeeId="+mId,\n "Employee Name="+mEname, \n "Company Name="+mCompanyname);

}

}

class EmployeeMain {

public static void main(String [] args){

Company mCompany1= new Company(1, "Raj");

Company mCompany2= new Company(2, "Rakesh");

mCompany1.display();

mCompany2.display();

}

}

Output:-

EmployeeId= 1 

Employee Name=  Raj

Company Name= GK InfoTech

EmployeeId= 2

Employee Name= Rakesh

Company Name= GK InfoTech

Post a Comment

0 Comments