return method

Return method should be the last statement, In method definition if we give any statement after return statement we will get “Unreachable Code” Compile time error.

Private Class

The private access modifier is specified using the keyword private.

  • The methods or data members declared as private are accessible only within the class in which they are declared.

Can we have private class?

No, we cannot have class no private.

why cant we have private class?

No other class can access it.

default – Package level access

having default access modifier are accessible only within the same package.

Only within the same package we can access. any variable/object or method. outside the package we cannot access.

Difference Between Private and Default

Private – Class level access.

default – Package level access.

Public Class

The public access modifier is specified using the keyword public.

Classes, methods or data members which are declared as public are accessible from every where in the program

public is an access modifier. this can be given for variables,objects,methods and classes.

if we declare anything (above) as public, it can be accessed from any where.

Constructor

To stored object specific information

Constructor is called automatically, when the object is created

Rule

It has the same name of our class name

No return type

It looks like method, but not. It does not any return data type

It looks like class, but not. because no class keyword is attached.

There are two type of constructor in Java:

1. Default constructor

A constructor that has no parameter is known as default constructor.

It is invisible.

(e.g) Box pencil = new Box();

2. Parameterized Constructor

A constructor that has parameters is known as parameterized constructor.

Overload constructor.

(e.g)

classGeek {

String name;

int id;

Geek(String name, intid)

{

this.name = name;

this.id = id;

}

}

Global Variables ( field ).

Static Variables – Class specific information

We can access static variables by using their class name

Instance variables – Instance ( object ) specific information for each and every instance, a copy will be maintained.

we can access instance variables by using instances ( Objects ).

this keyword

“this” keyword refers to current class object

“this” keyword can access all global instance variables

(e.g)

class Student{ 

 int rollno;  

String name;  

float fee;  

Student(int rollno,String name,float fee)

 this.rollno=rollno; 

 this.name=name; 

 this.fee=fee; 

 }  

}

Static keyword

The static keyword belongs to the class than an instance of the class.

Static refers to class level memory.

(e.g) main method ( public static void main)

when class is loaded into memory, main method is also getting loaded. thus it gets executed automatically.

Leave a comment