0% found this document useful (0 votes)
170 views116 pages

Durga Sir's Java Class Notes

The document provides an overview of classes, objects, static and non-static variables and methods in Java. It explains that classes are used to create objects using the new keyword. Static members belong to the class while non-static members belong to objects. Local variables are declared within methods and can only be accessed within the method, while static and non-static variables are declared at the class level. It also provides examples demonstrating the use of unary operators like increment/decrement and how methods are called.

Uploaded by

sai venkata
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views116 pages

Durga Sir's Java Class Notes

The document provides an overview of classes, objects, static and non-static variables and methods in Java. It explains that classes are used to create objects using the new keyword. Static members belong to the class while non-static members belong to objects. Local variables are declared within methods and can only be accessed within the method, while static and non-static variables are declared at the class level. It also provides examples demonstrating the use of unary operators like increment/decrement and how methods are called.

Uploaded by

sai venkata
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Class Basics
  • Static Members
  • Unary Operators in Java
  • Variables and Their Types
  • Type Casting
  • Method Definitions in Java
  • Constructors
  • Packages in Java
  • Access Specifiers
  • Polymorphism
  • Encapsulation in Java
  • Interfaces in Java
  • Empty Pages

Core Java Notes

Whats app number: 9632882052

Class:

1. Class helps us to create object.

2. Class will create object only when it receives request from new keyword.

new :

1. new keyword sends request to the class asking class to generate an object

2. Once the class has created an object new keyword will get the address of the object and will
store that in a special reference variable

Example 1:
public class A {

public static void main(String args[]) {

A a1 = new A();

A a2 = new A();

A a3 = new A();

[Link](a1);

[Link](a2);

[Link](a3);

Output:

A@7960847b

A@6a6824be

A@5c8da962

What is non static?

1. Non static members belongs to object. Whenever an object is created only non static member
gets loaded into the object.

Note: Static members never gets loaded into the object

Syntax to create an object:


ClassName variableName = new ClassName();

2. Non static members cannot be accessed / used without creating object

3. Every time we create an object. non static member will get loaded into it

Example 1:

public class A {

int i = 10;

public static void main(String[] args){

A a1 = new A();

[Link](a1.i);

Output:

10

What are static members ?

1. These members belongs to the class

2. To access static members do not create an object

3. Static members are accessed directly with class name

4. When a member is made static it gets loaded into the common memory of class

5. If a member is static then it will get loaded into the common memory only once throughout the
program

Example:

public class A {

int i = 10;

static int j = 100;

public static void main(String[] args){

[Link](A.j);
}

//if we are not using new keyword in the program, its means that no Object is crated

Example 1:

public class A {

int i = 10;

static int j = 100;

public static void main(String[] args){

A a1 = new A();

[Link](a1.i);

[Link](A.j);

Output:

10

100

Example 2:

public class A {

int i = 10;

int j = 100;

static int k = 500;

public static void main(String[] args){

A a1 = new A();

[Link](a1.i);

[Link](a1.j);

[Link](A.k);
}

Output:

10

100

500

Example 3:

public class A {

int i = 10;

public static void main(String[] args){

A a1 = new A();

[Link](a1.i);

A a2 = new A();

[Link](a2.i);

A a3 = new A();

[Link](a3.i);

//In the above program we are creating 3 objects, hence variable i will get loaded 3 times in the
object

Output:

10

10
10

Example 4:

public class A {

int i = 10;

static int j = 100;

public static void main(String[] args){

A a1 = new A();

[Link](a1.i);

A a2 = new A();

[Link](a2.i);

A a3 = new A();

[Link](a3.i);

[Link](A.j);

Output:

10

10

10

100

Unary Operators in Java

1. POST INCREMENT (++): Here we increment the values of the variable by one when next time we
see the same variable in our program
Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i++;

[Link](i);

[Link](j);

Output:

12

21

Example 2:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i++ + i++;

[Link](i);

[Link](j);

Output:

13

33
Example 3:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i++ + i++ + i++;

[Link](i);

[Link](j);

Output:

14

46

Example 4:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i + i++ ;

[Link](i);

[Link](j);

Output:

12

32
Example 5:

public class A {

public static void main(String args[]) {

int i = 5;

int j = i + i++ + i + i++ ;

[Link](i);

[Link](j);

Output:

22

2. PRE INCREMENT: Here we increment the value of the variable in same step

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + ++i;

[Link](i);

[Link](j);

Output:

12

23
Example 2:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + ++i + ++i;

[Link](i);

[Link](j);

Output:

13

36

Example 3:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + i + ++i;

[Link](i);

[Link](j);

Output:

12

34
Examples on post & pre increment together:

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + i++;

[Link](i);

[Link](j);

Output:

12

22

Example 2:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + ++i + i++ + i++;

[Link](i);

[Link](j);

Output:

14

47
Example 3:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i-- + i--;

[Link](i);

[Link](j);

Output:

19

Example 4:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i-- + --i + ++i;

[Link](i);

[Link](j);

Output:

10

40

Example 5:
public class A {

public static void main(String args[]) {

int i = 10;

[Link](i++);

[Link](++i);

[Link](--i);

[Link](i--);

[Link](i);

Output:

10

12

11

11

10

Example 6:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ - ++i;

[Link](i);

[Link](j);

}
Output:

12

-2

Overview of methods:

1. Methods will execute only when we call it

Example 1:

public class A {

public static void main(String args[]) {

A a1 = new A();

[Link]();

public void test(){

[Link](500);

Output:

500

Example 2:

public class A {

public static void main(String args[]) {

[Link]();

public static void test(){

[Link](500);

}
Output:
500

Types of Variables in Java

1. local variable

2. static variables

3. non static variables

4. reference variables

What are local variables:

1. Local variables are created inside a method

2. Local variables can be used only within created method

3. These variables are accessed directly with its name

4. Without initializing local variables if used then it will get you an error

Example 1:

public class A {

public static void main(String args[]) {

A a1 = new A();

[Link]();

public void test(){

int i = 10;

[Link](i);

Output:

10
Note: never use static keyword to create a variable inside a method:

Example 2:

public class A {

public static void main(String args[]) {

A a1 = new A();

[Link]();

public void test(){

static int i = 10;//Error

[Link](i);

Example 3:

public class A {

public static void main(String args[]) {

A a1 = new A();

[Link]();

[Link](i);//Error

public void test(){

int i = 10;

[Link](i);

Output:
Error

Example 4:

public class A {

public static void main(String args[]) {

int i = 10;

[Link](i);

Output:

10

Example 5:

Without initializing the variable if it is accessed then you would get an error

public class A {

public static void main(String args[]) {

int i ;

[Link](i);

Output:

Error

What are static variables?

1. static variables are created outside all the methods but inside a class with static keyword

2. Static variables belongs to the class and can be used anywhere in the class

3. If we do not initialize static variables then automatically depending on the data type some
default value will get stored in it

Example 1:
public class A {

static int i = 10;

public static void main(String args[]) {

[Link](A.i);

A a1 = new A();

[Link]();

public void test(){

[Link](A.i);

Output:

10

10

Example 2:

public class A {

static int i ; //0

public static void main(String args[]) {

[Link](A.i);

Output:

0
Example 3:

public class A {

static int i,j,k;

public static void main(String args[]) {

[Link](A.i);

[Link](A.j);

[Link](A.k);

Output:

What are non static variables ?

1. These variables are created outside all the methods but inside a class without using static
keyword

2. To access non static variables it is mandatory to create object

3. It is not mandatory to initialize non static variables, if we do not initialize then automatically
some default value will be stored in it

Example 1:

public class A {

int i = 10;

public static void main(String args[]) {

A a1 = new A();

[Link](a1.i);

}
}

Output:

10

Example 2:

public class A {

int i ;

public static void main(String args[]) {

A a1 = new A();

[Link](a1.i);

Output:

What are reference variables?

1. The main purpose of reference variable is to store objects address

2. The data type of a reference variable is class name

Syntax:

className variableName = new className();

Types of Reference variables:

1. Local reference variable:

a. These variables are created inside a method and should be used only within created method

Example 1:

public class A {

public static void main(String args[]) {

A a1 = new A();//Creating

[Link](a1);//Using in main
[Link]();//using in main

public void test(){

[Link](a1);//Outside the created method

Output:

/[Link]: error: cannot find symbol

[Link](a1);//Outside the created method

symbol: variable a1

location: class A

1 error

Example 2:

public class A {

public static void main(String args[]) {

A a1 ; //I am creating a reference vaiable here

[Link](a1);

Output:

/[Link]: error: variable a1 might not have been initialized

[Link](a1);

1 error

2. Static reference variable:


a. These variables are created outside all the methods but inside a class using static keyword

b. These variables can be used anywhere in the program

c. static reference variable by default will get initialized to null value if object is not created

Example 1:

public class A {

static A a1 = new A(); //Global visibility

public static void main(String args[]) {

[Link](a1);

[Link]();

public void test(){

[Link](a1);

Output:

A@7960847b

A@7960847b

Example 2:

public class A {

static A a1 ; //Global visibility

public static void main(String args[]) {

[Link](a1);

}
Output:

null

Data types in Java in Type casting

Data type Memory Size Default Value


byte 1 byte 0
short 2 bytes 0
int 4 bytes 0
long 8 bytes 0
float 4 bytes 0.0
double 8 bytes 0.0
boolean NA false
var(in JDK 1.10) NA NA
char 2 bytes Empty space
String (Class) Is Class Not A datatype null

Which data type I should use to store 10 digit mobile phone number?

Example 1:

public class A {

public static void main(String args[]) {

long mobilenumber = 9632882052L;

[Link](mobilenumber);

Output:

9632882052

Example :

public class A {
public static void main(String args[]) {

float i = 10.3F;

[Link](i);

Output:

10.3

var datatype in java ?

a. var data type can store any kind of value in it

b. var data type cannot be static variable

c. var data type cannot be non static variable

Example 1:

public class A {

public static void main(String args[]) {

var i = true;

var j = "Pankaj";

var k = true;

var z = 10.3;

[Link](i);

[Link](j);

[Link](k);

[Link](z);

Output:
true

Pankaj

true

10.3

Example 2:

public class A {

static var i = 10;

public static void main(String args[]) {

Output:

[Link]: error: 'var' is not allowed here

static var i = 10;

1 error

Example 3:

public class A {

var i = 10;

public static void main(String args[]) {

Output:

[Link]: error: 'var' is not allowed here

var i = 10;
^

1 error

Note: Printing multiple values in java single print statement:

Example:

public class A {

public static void main(String args[]) {

int i = 10;

int j = 100;

[Link](i+" "+j);

Output:

10 100

Type Casting:

Process of converting a particular data type into required data type is called as type casting.

1. Auto Up casting:

a. converting smaller data type to bigger data type is called as auto up casting

b. During up casting if data loss happens then we would get an error

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

long j = i;

[Link](j);

}
}

Output:

10

Example 2:

public class A {

public static void main(String args[]) {

long i = 10; //8 bytes

int j = i;

[Link](j);

Output:

Error because copying data from bigger memory to smaller memory is not an automated process

Example 3:

public class A {

public static void main(String args[]) {

float i = 10.3f; //4 bytes

double j = i;

[Link](j);

Output:

10.300000190734863

Example 4:

public class A {
public static void main(String args[]) {

float i = 10.3f; //4 bytes

long j = i;

[Link](j);

Output:

Error because during data copying .3 decimal value will be lost

2. Explicit Down casting:

a. Converting bigger data type to smaller data type is called as explicit down casting

b. During explicit down casting data loss might happen

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

byte j = (byte)i;

[Link](j);

Example 2:

public class A {

public static void main(String args[]) {

long i = 10;

int j = (int)i;

[Link](j);

}
}

Output:

10

Example 3:

public class A {

public static void main(String args[]) {

double i = 10.3;

float j = (float)i;

[Link](j);

Output:

10.3

Example 4:

public class A {

public static void main(String args[]) {

float i = 10.3f;

int j = (int)i;

[Link](j);

Output:

10, in this case .3 value is lost and only integer part is copied in variable j

Example 5:

public class A {

public static void main(String args[]) {

float i = 10.3f;
long j = (long)i;

[Link](j);

Output:

10

Example 6:

public class A {

public static void main(String args[]) {

long i = 9632882052L;

int j = (int)i;

[Link](j);

Output:

1042947460

Extra examples on auto conversion:

Example 1:

public class A {

public static void main(String args[]) {

int i = 'b';

[Link](i);

Output: 98

Example 2:
public class A {

public static void main(String args[]) {

int i = 'ए';

[Link](i);

Output:

2319

Example 3:

public class A {

public static void main(String args[]) {

int i = 'அ';

[Link](i);

Output:

2949

Steps to install JDK:

Step 1: Download jdk 1.8 from oracle website

Step 2: Double click on .exe file and keep clicking on next button

Step 3: After installation you will see a folder created in the following path with the name java

C:\Program Files\Java

Steps to install eclipse:

Step 1: Download eclipse from Google

Note: ensure you are downloading eclipse for java ee

Step 2: Extract the zipped file and then click on [Link]


Note: you will get a pop up window with the name work space launcher. In this window you can
give the path where you want all you java projects being created in eclipse to be stored in you
computer

Step to Configure JDK with eclipse:

Step 1: In eclipse go to windows>> then preferences

Step 2: Type installed JRE

Step 3: Click on add button

Step 4: Select Standard VM and click on next

Step 5: Browse JRE home by clicking on directory button and select the following path on your
computer:

C:\Program Files\Java\jdk1.8.0_144

Step 6: Click on finish and select the check box JDK 1.8 and click okay and close

Steps to create Java Project in eclipse:

Step 1: Go to file>> new >> Select Project

Step 2: In the wizards type java, then select java project and click on next and give project name
example: app1

Steps to create Java class in Java Project

Step 1: In src folder of your project right click and select class

Step 2: Give class Name example: A and click on finish

Shortcuts in eclipse:

1. type main and then press control + space bar+enter

2. Syso then press control + space bar

Methods in Java

Rules to develop methods:

Rule 1: Execution of the program in java starts from opening bracket of main method
Rule 2: When a method calling statement executes, control is transferred to the matching method

Rule 3: When closing bracket of user defined method runs, control is transferred back to the
matching calling statement

Rule 4: When closing bracket of main method runs the complete program execution would stop

Example 1:

package methods_example_1;

public class A {

public static void main(String[] args) {//Rule 1

A a1 = new A();

[Link]();//Rule 2

}//Rule 4 STOP the program execution

public void test() {//Control Come Here

[Link](100);//NO Rule

}//rule 3

Output:

100

Example 2:

package methods_example_1;

public class A {

public static void main(String[] args) {//Rule 1 START

[Link](5);//No Rule

A a1 = new A();//NO Rule


[Link]();//Rule 2

[Link](10);//No Rule

}//Rule 4

public void test() {//COmes here

[Link](100);//No Rule

Output:

100

10

Example 3:

package methods_example_1;

public class A {

public static void main(String[] args) {//Rule 1 STARTS

A a1 = new A();//NO Rule

a1.test1();//Rule 2

a1.test2();//Rule 2

}//Rule 4 STOPS

public void test1() {//Comes Here

[Link](5);//No Rule

}//Rule 3

public void test2() {//Comes here

[Link](10);//No Rule

}//Rule 3

Output:

5
10

Example 4:
package methods_example_1;

public class A {
public static void main(String[] args) {//Rule 1 STARTS HERE
A.test1();//Rule 2
A.test2();//Rule 2
}//Rule 4
public static void test1() {//Comes here
[Link](5);//No Rule Applied
}//Rule 3
public static void test2() {//Comes Here
[Link](10);//No Rule
}//Rule 3
}
Output:
5
10

Example 5
package methods_example_1;

public class A {
public static void main(String[] args) {//Rule STARTS HERE
A.test1();//Rule 2
}//Rule 4 STOPS
public static void test1() {//Comes here
[Link](5);//No Rule
A a1 = new A();//No Rule
a1.test2();//Rule 2
}//Rule 3
public void test2() {//Comes here
[Link](100);//No Rule
}//Rule 3
}
Output:
5
100

Example 6:
package methods_example_1;

public class A {
public static void main(String[] args) {//Rule 1 STARTS HERE
[Link](10);//NO Rule
A a1 = new A();//NO Rule
a1.test1();// Rule 2
[Link](15);// NO Rule
A a2 = new A();// No Rule
a2.test1();//Rule 2
}//Rule 4 STOPS

public void test1() {


[Link](50);// NO Rule
}//Rule 3
}
Output:
10
50
15
50
Example 7:

void: when a method is void then it means that the method cannot return any
value
package methods_example_1;

public class A {

public static void main(String[] args) {


A a1 = new A();
int i = [Link]();
[Link](i);
}

public void test() {


return 500;
}

}
Output:
Error, because void methods cannot return any value

Example 8:

For a method to return value it should not be void as shown in the below
program

package methods_example_1;

public class A {

public static void main(String[] args) {


A a1 = new A();
int i = [Link]();
[Link](i);
}

public int test() {


return 500;
}

Output:
500

Note: Control + 1

Example 9:
package methods_example_1;

public class A {

public static void main(String[] args) {


A a1 = new A();
char i = [Link]();
[Link](i);
}

public char test() {


return 'a';
}

}
Output:

Example 10:

package methods_example_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

double i = [Link]();

[Link](i);

public double test() {

return 10.3;

Output:

10.3

Example 11:

package methods_example_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

String i = [Link]();

[Link](i);
}

public String test() {

return "Pankaj Sir Academy";

Output:

Pankaj Sir Academy

What is the difference between using only "return" keyword in method and
using " return value" keyword

1. we can use only "return" keyword in void methods. return keyword in void
methods helps us to return the control back to the calling statement. Using
return keyword in void methods is optional

Example 1:

package methods_example_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test() {

return;//We are returning the control back to the calling


statement

Note: After return keyword if we write any java code then that code will
never execute. Such java code will give us an error "Unreachable" statement
Example 2:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test(){

return;

[Link](100);//Never Execute

Output:

Error

Example 3:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test(){

[Link](100);//Never Execute
return;

Output:

100

Example 4:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

int i = [Link]();

[Link](i);

public int test(){

return 500;

[Link](100);//Never Execute

Output:

Unreachable Code Error

Example 5:

package methods_examples_1;

public class A {
public static void main(String[] args) {

A a1 = new A();

int i = [Link]();

[Link](i);

public int test(){

[Link](100);//Never Execute

return 500;

Output:

100

500

Example 6:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link](100, 200);

public void test(int i, int j){

[Link](i);

[Link](j);

}
}

Output

100
200

Example 7:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link](100, "Pankaj",'p',true, 10.3);

public void test(int i, String s, char c, boolean b, double d ){

[Link](i);

[Link](s);

[Link](c);

[Link](b);

[Link](d);

Output:

100

Pankaj

true

10.3

Example 8:

package methods_examples_1;
public class A {

public static void main(String[] args) {

A a1 = new A();

[Link](100,200,300,400,500);

public void test(int... x){

[Link](x[0]);

[Link](x[1]);

[Link](x[2]);

[Link](x[3]);

[Link](x[4]);

Output:

100

200

300

400

500

Method Definition: Methods helps us to break our programs into modules and
the code in the module can called any number of times which gives us
reusability of the code

Example 9:

package methods_examples_1;

public class A {

public static void main(String[] args) {

return;

}
Output:

Compile

Constructors In Java

a. Constructors should have same name as that of class

b. Whenever we create an object constructor will be called

c. We can create more than one constructor in the same class provided they
have different number of arguments or different type of arguments

d. By default constructors are internally void

e. When an object is created with no args, then if we do not create a


constructor automatically empty body constructor gets added up in .class
file

Example 1:

package methods_examples_1;

public class A {

A(){

[Link]("From Constructor A");

public static void main(String[] args) {

A a1 = new A();

A a2 = new A();

Output:

From Constructor A

From Constructor A

Example 2:

package methods_examples_1;
public class A {

A(int i){

[Link](i);

public static void main(String[] args) {

A a1 = new A(500);

Output:

500

Example 3:

note: In the below program when we add void before the constructor then
that's no longer treated as constructor, it would be treated as a method

package methods_examples_1;

public class A {

void A(){

[Link]("From Constructor A");

public static void main(String[] args) {

A a1 = new A();

Output:

Will Compile, but will print nothing

Example 4:

package methods_examples_1;
public class A {

void A(){

[Link]("From Constructor A");

public static void main(String[] args) {

A a1 = new A();

a1.A();

Output:

From Constructor A

Example 5:

package methods_examples_1;

public class A {

A(){// No Of Args is ZERO

[Link]("From Constructor One");

A(){// No Of Args is ZERO

[Link]("From Constructor One");

public static void main(String[] args) {

A a1 = new A();

Output:
Error because in the above program both of the constructor have same number
of arguments

Example 6:

package methods_examples_1;

public class A {

A(){// No Of Args is ZERO

[Link]("From Constructor One");

A(int i){// No Of Args is ONE

[Link](i);

public static void main(String[] args) {

A a1 = new A();

A a2 = new A(100);

Output:

From Constructor One

100

Example 7:

package methods_examples_1;

public class A {

A(){// No Of Args is ZERO

[Link]("From Constructor One");

A(int i){// No Of Args is ONE

[Link](i);

A(int i,int j){// No Of Args is TWO


[Link](i);

[Link](j);

public static void main(String[] args) {

A a1 = new A();

A a2 = new A(100);

A a3 = new A(500,300);

Output:

From Constructor One

100

500

300

Example 8:

package methods_examples_1;

public class A {

A(int i){//No Of args=1 and type=int

[Link](i);

A(char j){//No Of args=1 and type=char

[Link](j);

public static void main(String[] args) {

A a1 = new A(100);

A a2 = new A('a');

Output:
100

Example 9:

package methods_examples_1;

public class A {

A(int i){//No Of args=1 and type=int

[Link](i);

A(char j){//No Of args=1 and type=char

[Link](j);

A(String i){

[Link](i);

public static void main(String[] args) {

A a1 = new A(100);

A a2 = new A('a');

A a3 = new A("Pankaj Sir Academy");

Output:

100

Pankaj Sir Academy

Example 10:

package constructor_example;

public class A {

A(){
return 100;

public static void main(String[] args) {

A a1 = new A();

Output:

Error

Example 11:

package constructor_example;

public class A {

A() {

[Link](100);

return;

public static void main(String[] args) {

A a1 = new A();

Output:

100

Example 12:

package constructor_example;

public class A {
public static void main(String[] args) {

A a1 = new A(100);

Output:

Error, because it is mandatory to create a constructor when an object with


argument is created

this keyword

a. this keyword is a special reference variable in java that holds current


objects address

b. using this keyword we can access non static members of the class

c. We cannot use this keyword in static methods

d. this keyword gets added automatically in non static methods while access
the objects member

e. Using this keyword we can call current class constructor, but the call
has be from another constructor

f. Using this keyword when you call a constructor it has to be the first
statement inside another construtor

Example 1:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link](a1);

[Link]();

public void test(){

[Link](this);
}

Output:

constructor_example.A@15db9742

constructor_example.A@15db9742

Example 2:

package constructor_example;

public class A {

int i = 10;

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

public void test(){

[Link](this.i);

Output:

10

10

Example 3:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();
a1.test1();

public void test1(){

[Link]("From test 1");

this.test2();

public void test2(){

[Link]("From test 2");

Output:

From test 1

From test 2

Example 4:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link](this);

Output:

Error because we cannot use this keyword in static method

Example 5:

package constructor_example;

public class A {
public static void main(String[] args) {

A a1 = new A();

[Link]();

public static void test(){

[Link](this);

Output:

error because we cannot use this keyword in static method

Example 6:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();

[Link](a1);

[Link]();

A a2 = new A();

[Link](a2);

[Link]();

public void test(){

[Link](this);

Output:
constructor_example.A@15db9742

constructor_example.A@15db9742

constructor_example.A@6d06d69c

constructor_example.A@6d06d69c

Example 7:

package constructor_example;

public class A {

static int i = 10;

int j = 100;

public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test(){

[Link](this.i);

[Link](this.j);

Output:

10

100

Example 8:

package constructor_example;

public class A {

int i = 100;

public static void main(String[] args) {

A a1 = new A();
[Link]();

public void test(){

[Link](i);

//this keyword gets added automatically

//in non static methods while

//access the objects member

Output

100

Example 9:

package constructor_example;

public class A {

int i = 100;

public static void main(String[] args) {

A a1 = new A();

[Link]();

public static void test(){

[Link](i);

//this keyword cannot get added automatically

//in static methods while

//access the objects member

Output:
Error

Example 10:

package constructor_example;

public class A {

int i = 100;

public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test(){

[Link](i);

x();//this keyword is added automatically

public void x(){

[Link]("From x");

Output:

100

From x

Example 11:

package this_examples;

public class A {

A(){

[Link]("From Constructor A");

A(int i){
this();

public static void main(String[] args) {

A a1 = new A(100);

Output:

From Constructor A

Example 12:

While calling a constructor using this keyword, it cannot be second


statement inside another constructor

package this_examples;

public class A {

A(){

[Link]("From Constructor A");

A(int i){

[Link](i);

this();

public static void main(String[] args) {

A a1 = new A(100);

}
}

Output:

Error because this keyword is second statement

Example 12:

package this_examples;

public class A {

A(){

[Link]("From Constructor A");

A(int i){

this();

[Link](i);

public static void main(String[] args) {

A a1 = new A(100);

Output:

From Constructor A

100

Example 13:

package this_examples;

public class A {

A(){
this(100);

A(int i){

[Link](i);

public static void main(String[] args) {

A a1 = new A();

Output:

100

Note:

Local variable name and non static variable name can be same

Example 1

package this_examples;

public class A {

int i = 20;//Non static

public static void main(String[] args) {

int i = 10;//Local Variable

Output:

Will Compile but will print nothing


Note: Using this keyword we cannot access local variables

Example 1:

package this_examples;

public class A {

public static void main(String[] args) {

public void test(){

int i = 10;

[Link](this.i);

Output:

Error because we cannot use this keyword to access local variables

Example 2:

package this_examples;

public class A {

int i;//0

public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test(){

int i = 10;

[Link](this.i);
}

Output:

Example 3:

package this_examples;

public class A {

int i;//0

public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test(){

int i = 10;

i = this.i;

[Link](i);

Output:

Example 4:

package this_examples;

public class A {

int i;//0
public static void main(String[] args) {

A a1 = new A();

[Link]();

public void test(){

int i = 10;

this.i = i;

[Link](i);

[Link](this.i);

Output:

10

10

Instance Initialization Block (IIB)

a. IIB will be called whenever an object is created

b. When there is more than one IIB then these IIB's will run in sequence
one after another when an object is created

c. Always IIB runs first and then the constructor

Example 1:

public class A {

[Link](100);

public static void main(String[] args) {

A a1 = new A();

A a2 = new A();
A a3 = new A();

Output:

100

100

100

Example 2:

package iib_examples;

public class A {

[Link](50);

[Link](100);

[Link](500);

public static void main(String[] args) {

A a1 = new A();

Output:

50

100
500

Example 3:

package iib_examples;

public class A {

A(){

[Link](100);

[Link](50);

public static void main(String[] args) {

A a1 = new A();

Output:

50

100

Example 4:

package iib_examples;

public class A {

[Link](20);

A(){
[Link](100);

[Link](50);

public static void main(String[] args) {

A a1 = new A();

Output:

20

50

100

Example 5:

package iib_examples;

public class A {

[Link](20);

A(int i){

[Link](i);

[Link](50);

}
public static void main(String[] args) {

A a1 = new A(100);

Output:

20

50

100

static initialization block (SIB):

a. SIB runs automatically and it runs before main method

b. If we have more than one SIB in the program then it will run in sequence
one after another

Example 1:

package iib_examples;

public class A {

static {

[Link](100);

public static void main(String[] args) {

[Link](50);

Output:

100
Example 2:

package iib_examples;

public class A {

static {

[Link](100);

Output:

Error, Because main method is mandatory to run a program

Example 3:

package iib_examples;

public class A {

static {

[Link](100);

static{

[Link](500);

public static void main(String[] args) {

[Link](1000);

Output:

100

500

1000

Interview Question:
Write a java program to call main method twice?

Example 1:

public class A {

static {

[Link](null);

[Link](null);

[Link](null);

public static void main(String args[]) {

[Link](500);

Output:

500

500

500

500

Example 1:

package sib_iib_constructors_example;

public class A {

[Link](5);

static{

[Link](10);

A(){

[Link](100);

}
public static void main(String[] args) {

[Link](500);

Output:

10

500

Example 2:

Note: Always static block will run first and then main method, and if
object is created then Instance initialization block will run and finally
constructor would run.

package sib_iib_constructors_example;

public class A {

[Link](5);

static{

[Link](10);

A(){

[Link](100);

public static void main(String[] args) {

[Link](500);

A a1 = new A();

Output:

10

500

5
100

Example 2:

package sib_iib_constructors_example;

public class A {

[Link](5);

static{

[Link](10);

A(){

[Link](100);

[Link](1);

public static void main(String[] args) {

[Link](500);

A a1 = new A();

Output:

10

500

100

Example 3:

package sib_iib_constructors_example;

public class A {
static {

[Link](1000);

[Link](5);

static{

[Link](25);

[Link](9);

A(){

[Link](11);

public static void main(String[] args) {

A a1 = new A();

[Link](600);

Output:

1000

25

11

600

Example 4:

package sib_iib_constructors_example;

public class A {
static {

A a1 = new A();

[Link](5);

A(){

[Link](10);

public static void main(String[] args) {

Output:

10

Purpose of IIB?

Whenever an object is created lets initialize all non static variables in


one place and that will give us better clarity of code

Example 1:

package sib_iib_constructors_example;

public class A {

int i,j,k;

i = 10;

j = 20;

k = 30;

}
public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link](a1.j);

[Link](a1.k);

Output:

10

20

30

Note:

In IIB both the non static variables and static variables can be
initialized

Example 2:

package sib_iib_constructors_example;

public class A {

int i;

static int j;

i = 10;

j = 100;

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link](A.j);

}
}

10

100

What is the purpose of SIB?

All static variables are initialized in SIB for better clarity of the code

Example 1:

package sib_iib_constructors_example;

public class A {

static int i,j,k;

static{

i = 10;

j = 20;

k = 30;

public static void main(String[] args) {

[Link](A.i);

[Link](A.j);

[Link](A.k);

Output:

10

20

30

Note:

In SIB we can never initialize non static variables

Example 2:
package sib_iib_constructors_example;

public class A {

int i,j,k;

static{

i = 10;

j = 20;

k = 30;

public static void main(String[] args) {

[Link](A.i);

[Link](A.j);

[Link](A.k);

Output

Error

OOPS

1. inheritance

2. Polymorphism

3. encapsulation

4. abstraction

Inheritance:

a. Here we inherit non static members of parent class into child class
object

Example 1:

package inheritance_examples;

public class A {

int i = 10;
}

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link](b1.i);

Output:

10

Example 2:

public class A {

int i = 10;

public void test(){

[Link](1000);

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link](b1.i);

[Link]();

Output:

10

1000

Note: In the below program class A and class B are non sub class

Example 3:
package inheritance_examples;

public class A {

int i = 10;

public void test(){

[Link](1000);

package inheritance_examples;

public class B{//Class B and class A are non sub class

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Output:

10

1000

Example 4:

package inheritance_examples;

public class A {

public void test1(){

[Link](1000);

package inheritance_examples;

public class B extends A{//test1() + test2()

public void test2(){

[Link](500);

}
}

package inheritance_examples;

public class C extends B{//test1() + test2()

public void test3(){

[Link](10);

public static void main(String[] args) {

C c1 = new C();

c1.test1();

c1.test2();

c1.test3();

Output:

1000

500

10

Example 5:

Note: In java at class level multiple inheritance is not allowed. Multiple


inheritance makes the design of your software very complex

The below program throws an error, because of multiple inheritance

package inheritance_examples;

public class A {

package inheritance_examples;
public class B {

package inheritance_examples;

public class C extends A,B{//Error

Output:

Error

Note:

static members are not inherited, but it gives us a feel of inheritance in


the following program by converting B.i to A.i

Example 6:

package inheritance_examples;

public class A {

static int i;

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

[Link](B.i);//A.i

Output:

0
Example 7:

Note: static members are not inherited, but it gives us a feel of


inheritance in the following program by converting B.i to A.i and [Link]()
to [Link]()

package inheritance_examples;

public class A {

static int i;

public static void test(){

[Link]("From Test");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

[Link](B.i);//A.i

[Link]();//[Link]()

Output:

From Test

Example 8:

package inheritance_examples;

public class A {

static int i;
public static void test(){

[Link]("From Test");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link](b1.i);//A.i

[Link]();//[Link]();

Output:

From Test

Super keyword in Java

a. Using super keyword we can access members of parent class

b. We cannot use super keyword in static methods

c. Using super keyword we can call constructor of parent class, but the
call has to be made from child class constructor

d. super keyword cannot be second statement inside child class constructor


while calling parent class constructor

Example 1:

package inheritance_examples;

public class A {

int i;

}
package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link]();

public void test(){

[Link](super.i);

Output:

Example 2:

package inheritance_examples;

public class A {

int i;

public void x(){

[Link]("x");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();
[Link]();

public void test(){

[Link](super.i);

super.x();

Output:

Example 3:

package inheritance_examples;

public class A {

int i;

public void x(){

[Link]("x");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link]();

public static void test(){

[Link](super.i);
super.x();

Output:

Error because we should not use super keyword inside a static method.

Example 4:

Note: Using super keyword we can access static and non static members, but
it is advisable that we should not access static members with super keyword

package inheritance_examples;

public class A {

static int i;

public static void x(){

[Link]("x");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link]();

public void test(){

[Link](super.i);

super.x();

}
Output:

Example 5:

package inheritance_examples;

public class A {

A(){

[Link]("From Consructor A");

package inheritance_examples;

public class B extends A{

B(){

super();

public static void main(String[] args) {

B b1 = new B();

Output:

From Consructor A

Example 6:

package inheritance_examples;

public class A {

A(int i){
[Link](i);

package inheritance_examples;

public class B extends A{

B(){

super(100);

public static void main(String[] args) {

B b1 = new B();

Output:

100

Example 7:

package inheritance_examples;

public class A {

A(int i){

[Link](i);

package inheritance_examples;

public class B extends A{

B(){

[Link](500);
super(100);

public static void main(String[] args) {

B b1 = new B();

Output:

Error because super keyword cannot be second statement inside child class
constructor while calling parent class constructor

Example 8:

package inheritance_examples;

public class A {

A(){

[Link](100);

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

Output:

The above program will print 100 because during compilation java compiler
will automatically create constructor with no args and super keyword in
.class file of B
Packages In Java

Definition: Packages are nothing but folders created in java to store your
programs in organized manner

Packages resolves class naming convention

Example 1:

package p1;

public class A {

In the above package keyword defines that class A is created in folder p1.

Note:

If you want to access a class present in different package then we have to


firstly import that class and only then use that as shown in the below
program!!

Example 1:

package p1;

public class A {

package p2;

import p1.A;

public class B extends A{

public static void main(String[] args) {

Example 2:

package p1;

public class A {

package p1;
public class C extends A{

public static void main(String[] args) {

Example 3:

package p1;

public class A {

package p2;

public class B extends p1.A{

public static void main(String[] args) {

p1.A a1 = new p1.A();

Example 4:

package p3.p4.p5;

public class D {

Example 5:

package p2;

import p3.p4.p5.D;

public class B extends D{

public static void main(String[] args) {

D d1 = new D();

}
}

package p3.p4.p5;

public class D {

Example 6:

package p1;

public class A {

package p1;

public class C extends A{

public static void main(String[] args) {

package p2;

import p1.A;

import p1.C;

public class B {

public static void main(String[] args) {

A a1 = new A();

C c1 = new C();

Example 7:

package p1;

public class A {
}

package p1;

public class C extends A{

public static void main(String[] args) {

package p2;

import p1.*;

public class B {

public static void main(String[] args) {

A a1 = new A();

C c1 = new C();

Access Specifier / Access Modifiers

1. private

2. default

3. protected

4. public

private default protected public


Same class yes Yes yes yes

Same package
subclass NO Yes yes yes

Same package
non subclass NO Yes yes yes
different
package No No yes yes
subclass

different
package non No No No yes
subclass

Private:

a. private members are the class are accessible in the same class

Example 1:

package p1;

public class A {

private int i = 10;

private void test(){

[Link]("From test");

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Output:

10

From test

Example 2:

private members cannot be accesses in same package sub class

package p1;

public class A {

private int i = 10;

private void test(){


[Link]("From test");

package p1;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link](b1.i);

[Link]();

Output:

error

Note: private members cannot be accessed in different non subclass same


package

Example 3:

package p1;

public class A {

private int i = 10;

private void test(){

[Link]("From test");

package p1;

public class B {

public static void main(String[] args) {


A a1 = new A();

[Link](a1.i);

[Link]();

Output:

error

Note: private members cannot be accessed in different package sub class

Example 4:

package p1;

public class A {

private int i = 10;

private void test(){

[Link]("From test");

package p2;

import p1.A;

public class C extends A{

public static void main(String[] args) {

C c1 = new C();

[Link](c1.i);

[Link]();

Output:

Error
Note: private members cannot be accessed in different package non sub class

Example 5:

package p1;

public class A {

private int i = 10;

private void test(){

[Link]("From test");

package p2;

import p1.A;

public class C {

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Output:

Error

Important Note:

Private members are accessible only in same class, outside that class
private members cannot be used

default access specifier:

These members are accessible only in same package, outside the package tese
members cannot be accessed

Example 1:

package p1;
public class A {

int i = 10;

void test(){

[Link]("From Test");

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Output:

10

From Test

Example 2:

package p1;

public class A {

int i = 10;

void test(){

[Link]("From Test");

package p1;
public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link](b1.i);

[Link]();

Output:

10

From Test

Example 3:

package p1;

public class A {

int i = 10;

void test(){

[Link]("From Test");

package p1;

public class B {

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);
[Link]();

Output:

10

From Test

Example 4:

package p1;

public class A {

int i = 10;

void test(){

[Link]("From Test");

package p1;

public class A {

int i = 10;

void test(){

[Link]("From Test");

}
Example 5:

package p1;

public class A {

int i = 10;

void test(){

[Link]("From Test");

package p2;

import p1.A;

public class C {

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Protected Access Specifier:

protected members can be accessed in same package and in different package


onlythrough inheritance

Example 1:

package p1;

public class A {

protected int i = 10;

protected void test(){


[Link]("From Test");

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Output:

10

From Test

Example 2:

package p1;

public class A {

protected int i = 10;

protected void test(){

[Link]("From Test");

package p1;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

[Link](b1.i);
[Link]();

Example 3:

package p1;

public class A {

protected int i = 10;

protected void test(){

[Link]("From Test");

package p1;

public class B {

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Example 4:

package p1;

public class A {
protected int i = 10;

protected void test(){

[Link]("From Test");

package p2;

import p1.A;

public class C extends A{

public static void main(String[] args) {

C c1 = new C();

[Link](c1.i);

[Link]();

Output:

10

From Test

Example 5:

package p1;

public class A {

protected int i = 10;

protected void test(){


[Link]("From Test");

package p2;

import p1.A;

public class C{

public static void main(String[] args) {

A a1 = new A();

[Link](a1.i);

[Link]();

Output:

Error

Public access specifier:

Public access specifier can be accessed anywhere in the project

Note: A class in java can be public or default. It cannot be private or


protected

Example 1:

In the below example we get an error because default class cannot be


accessed in different package

package p1;

class A {

}
package p2;

import p1.A;

public class B extends A{

Output: Error

Note:

1. If a constructor is made public then its object can be created anywhere


in the project

2. If a constructor is made default then its object cannot be created in


different package

3. If a constructor is made private then its object can be created in same


class

4. If a constructor is made protected then its object can be created in


same package only just like default constructor

Polymorphism In Java:

When we develop a feature in java that can take more than one form is
called as polymorphism.

Their two ways to achieve polymorphism:

a. Overriding:

Note: How to modify the logic of inherited method in child class:

Definition: In Overriding we inherit a method from parent class and then we


modify the logic of inherited method in child class by once again creating
a method with same signature in child class

Note: Polymorphisms applied only on methods

Example 1:

package polymorphism_examples;

public class A {

public void test(){


[Link]("Hello");

package polymorphism_examples;

public class B extends A{

public void test(){

[Link]("world");

public static void main(String[] args) {

B b1 = new B();

[Link]();

Output:

world

Example 2:

package polymorphism_examples;

public class A {

public void test(){

[Link]("Hello");

package polymorphism_examples;
public class B extends A{

public void test(){

[Link]("world");

public static void main(String[] args) {

B b1 = new B();

[Link]();

A a1 = new A();

[Link]();

Output:

world

Hello

Note:

While overriding it is advised that we use the annotation @Override. This


annotation will check whether overriding is happening or not

Example 3:

package polymorphism_examples;

public class A {

public void test(){

[Link]("Hello");

}
package polymorphism_examples;

public class B extends A{

@Override

public void tests(){

[Link]("world");

public static void main(String[] args) {

B b1 = new B();

[Link]();

Output:

Error because parent class method name and child's class method names are
not matching

Example 4:

package polymorphism_examples;

public class A {

public void test1(){

[Link]("Hello");

public void test2(){

[Link]("Welcome");

package polymorphism_examples;

public class B extends A{


@Override

public void test1(){

[Link]("world");

public static void main(String[] args) {

B b1 = new B();

b1.test1();

b1.test2();

Output:

world

Welcome

Example 5:

package banking_app;

public class GoldAccount {

public void onlineBanking(){

[Link]("yes");

public void chqBooks(){

[Link]("2/year");

public void interest(){

[Link]("No Interest on Balance");

}
public void minBalance(){

[Link]("Rs. 5000");

package banking_app;

public class PlatinumAccount extends GoldAccount{

public void chqBooks(){

[Link]("Unlimited/year");

public void interest(){

[Link]("4%/year");

public void minBalance(){

[Link]("Rs. 10000");

public static void main(String[] args) {

GoldAccount g = new GoldAccount();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]("______________");

PlatinumAccount p = new PlatinumAccount();

[Link]();

[Link]();

[Link]();

[Link]();
}

Output:

yes

2/year

No Interest on Balance

Rs. 5000

______________

yes

Unlimited/year

4%/year

Rs. 10000

b. Overloading:

Definition: Here we develop multiple methods with the same name but having
different number of arguments or different type of arguments

public class Email {

//Marketing Mailers

//Transactional Mailers

public void sendEmail(String un, String pwd, String route){

[Link]("Will send marketing emailers");

public void sendEmail(String un, String pwd, String route,String


tcid){

[Link]("Will send transaction emailers");

public static void main(String[] args) {

Email e = new Email();

[Link]("xx", "xx", "marketing");


[Link]("xx", "xx", "transactional","12567");

Output:

Will send marketing emailers

Will send transaction emailers

Encapsulation in Java

encapsulation refers to the bundling of data(variables) with the methods


that operate on that data and restricting of direct access of
data(variables).

TO achieve encapsulation we should do the following

1. Make the variables private so that they cannot be accessed directly

2. Create getters and setters to access the data(variables)

Example:

package encapsulation_example;

//We will build methods that will initialize variable i and

//also read the data of variable i

public class A {

private int i ;

private String name;

public int getI() {

return i;

public void setI(int i) {

this.i = i;

public String getName() {


return name;

public void setName(String name) {

[Link] = name;

package encapsulation_example;

public class B {

public static void main(String[] args) {

A a1 = new A();

[Link](100);

[Link]([Link]());

[Link]("Pankaj Sir Academy");

[Link]([Link]());

Output:

100

Pankaj Sir Academy

Interfaces in Java

a. In an interface we can create only incomplete methods in it. We can also


call incomplete methods in an interface as abstract methods

b. When a class implements an interface then the class has to complete all
incomplete methods of an interface in the class or else you would get an
error.

Example 1:

package java_app_1;
public interface B {

public void x() ;

Note: The above interface is stored as "[Link]"

Example 2:

package java_app_2;

public interface A {

public void test();

package java_app_2;

public class B implements A{

public void test() {

[Link]("From Test");

public static void main(String[] args) {

B b1 = new B();

[Link]();

Output:

From Test

Example 3:

package java_app_3;

public interface A {

public void test1();

public void test2();


}

package java_app_3;

public class B implements A{

@Override

public void test1() {

[Link]("From test1");

@Override

public void test2() {

[Link]("From test2");

public static void main(String[] args) {

B b1 = new B();

b1.test1();

b1.test2();

Output:

From test1

From test2

Common questions

Powered by AI

Static variables in Java are associated with the class itself rather than any particular object instance, which means they are shared across all instances of the class. These variables are stored in the static memory of the class and retain their value between method calls, as demonstrated by their ability to maintain state across different instantiations of an object . Non-static variables, on the other hand, are instance variables that pertain to individual object instances and thus, each instance has its own copy . From a memory usage perspective, static variables can reduce memory overhead since they are not duplicated across instances, but care must be taken to avoid unintended state sharing across instances. From a program design perspective, static variables can facilitate shared data management without creating unnecessary objects, while non-static variables support encapsulation and instance-specific state management .

Access specifiers in Java—private, default, protected, and public—dictate the accessibility of classes and class members, directly influencing code encapsulation and security. Private members are accessible only within the class they are declared, ensuring the highest level of data hiding and encapsulation as they cannot be accessed by derived classes or other packages . Default access allows visibility within the same package but not outside it, providing package-level encapsulation . Protected access allows subclasses, even in different packages, to access members, thus facilitating controlled inheritance . Public access grants visibility from any other code in the same or different packages, ensuring the most flexibility but the least encapsulation . These specifiers help in defining strict boundaries to prevent unintended use and modification of data, thus increasing security by controlling the exposure of the internal state and implementation details of a class .

Java's package system plays a crucial role in managing namespace conflicts and organizing design within larger projects by encapsulating related classes and interfaces into separate namespaces. This allows for the use of classes with the same name in different packages without conflict. For instance, two classes named 'A' can coexist if one is defined in package 'p1' and another in package 'p2' . Packages facilitate modular design by categorizing classes based on their functionality or module, thus simplifying maintenance and improving collaboration in large development teams by clearly defining boundaries . They also enforce access control and encapsulation, as only classes within the same package can access members without being declared public . This framework not only prevents naming conflicts but also encourages a structured approach to project organization, clearly defined dependencies, and improved code readability .

In Java, combining inheritance with protected access specifiers enhances component reusability while balancing security concerns. The 'protected' access modifier allows subclass access to superclass members, facilitating code reuse, as seen when classes are extended across packages . For example, if class B extends class A, protected members in A are accessible within B, allowing B to reuse and adapt A's functionality. However, protected ensures that members are not exposed to all other classes, maintaining security by restricting access to subclasses and classes within the same package only. It ensures that only components which extend or closely relate to the protected data can modify or use it, thus reducing unintentional misuse or modification from unrelated parts of a codebase, preserving both encapsulation and the integrity of inherited components .

In Java, methods are pieces of code that run only when explicitly called or invoked. This is reflected in how Java handles methods involving conditional calls or external invocation. For instance, consider the following example: class A { public static void main(String[] args) { A a1 = new A(); a1.test(); } public void test(){ System.out.println(500); } } The 'test' method is only executed when 'a1.test()' is called within the main method, leading to the output "500". Without the explicit method invocation, none of the code within 'test' would execute . This structured invocation requirement ensures that methods execute in a controlled manner, preventing unintentional code execution and allowing developers to define clear entry points for different functionality within a program.

Local reference variables and static reference variables in Java serve different scopes and purposes. A local reference variable is created inside methods, and its existence is limited to the method's execution scope, which promotes encapsulation and program segmentation localized to specific functionality . For example, within a method, once the execution exits the method, the local reference variable cannot be used and is eligible for garbage collection. Static reference variables, on the other hand, are declared outside methods with the 'static' keyword and are available across the class and every instance of it. They retain their reference throughout the lifetime of the application, making them useful for shared object manipulation or to avoid redundancy in resource-intensive instances . The encapsulation provided by local reference variables promotes security and reduces side effects by limiting data exposure to small parts of a program. Meanwhile, static reference variables improve cohesion by ensuring consistent object states across the program. However, they demand careful handling to avoid unintended data sharing across unrelated contexts .

Unary operators such as increment (++ and --) significantly influence expression evaluation and value assignment in Java. When used in expressions, these operators either increase or decrease a variable's value by one. Post-increment (i++) and post-decrement (i--) use the variable's current value in an expression before changing the value, whereas pre-increment (++i) and pre-decrement (--i) modify the variable's value first before using it in the expression . For example, if i = 10, the expression 'int j = i++ + ++i;' initially uses i's value (10) before incrementing it after the addition operation. In contrast, '++i' increases i to 12 before it is used, resulting in j being assigned the sum of 10 and 12, which is 22 . This underlying mechanism affects how developers plan their calculations and manipulations within programs, emphasizing order of operations and understanding of side effects caused by value changes .

The main difference between post-increment (i++) and pre-increment (++i) operators is the sequence in which the increment and the operation is executed. In post-increment, the current value is used in the expression, and then the variable is incremented. Conversely, in pre-increment, the variable is incremented first, and the incremented value is used in the expression . For example, in the statement 'int j = i++ + i++;', j takes on the value 21 because the current value of i, which is 10, is used in both operations before i is incremented twice . In contrast, 'int j = ++i + ++i;' results in j being 23, as i is incremented to 11 before the first use and then again to 12 before the second use . These differences affect program outputs when the sequence of operations depends on the current versus incremented values of variables.

Java's type system, through its rigorous definition of data types, their sizes, and default values, significantly impacts performance and memory management. Each primitive data type in Java has a fixed size: int (4 bytes), float (4 bytes), double (8 bytes), etc. This predictability allows for efficient memory allocation, helping programs to manage resources effectively . Default values are provided for uninitialized variables—such as 0 for integers and 0.0 for floats—preventing undefined behavior and ensuring that memory is not left in a potentially insecure initial state . By enforcing strict typing and memory allocation rules, Java reduces overhead associated with resource-intensive memory management. This is particularly important given Java's platform-independent nature, ensuring consistent performance across different computational environments . Additionally, knowing default values aids in debugging and improves reliability by reducing null pointer exceptions and other runtime errors related to uninitialized data.

The 'super' keyword in Java acts as a reference to the immediate parent class object and plays a pivotal role in constructor execution within Java's inheritance model. According to Java's rules, 'super' must be the first statement in the constructor when a parent class's constructor is called from a subclass. This ensures that the initialization sequence starts from the top of the inheritance hierarchy and proceeds downward, allowing parent class properties to be established before extending functionality in the subclass . For example, in 'class B extends A { B() { super(i); } }', the 'super' keyword is used to call A's constructor first . Violating these rules, such as placing 'super' anywhere except as the first statement, results in a compilation error, as shown when trying to call 'super' after any other statement in a constructor . This rule ensures a structured and predictable inheritance chain, maintaining consistency in object state initialization while reducing the risk of accessing uninitialized parent class components within subclass constructors.

Core Java Notes 
Whats app number: 9632882052 
Class:  
1. Class helps us to create object.  
2. Class will create object o
ClassName variableName = new ClassName(); 
2. Non static members cannot be accessed / used without creating object 
3. Ever
} 
} 
//if we are not using new keyword in the program, its means that no Object is crated 
Example 1: 
public class A {
} 
} 
Output: 
10 
100 
500 
Example 3: 
public class A { 
   int i = 10; 
  
   public static void main(String
10 
Example 4: 
public class A { 
   int i = 10; 
   static int j = 100;     
 
   public static void main(String[] args){
Example 1: 
public class A { 
    public static void main(String args[]) { 
    int i = 10; 
    int j = i++   +  i++;
Example 3: 
public class A { 
    public static void main(String args[]) { 
    int i = 10; 
    int j = i++   +  i++   +
Example 5: 
public class A { 
    public static void main(String args[]) { 
    int i = 5; 
    int j = i   +  i++ + i   +
Example 2: 
public class A { 
    public static void main(String args[]) { 
    int i = 10; 
    int j = ++i  +   ++i  +  +
Examples on post & pre increment together: 
Example 1: 
public class A { 
    public static void main(String args[]) {

You might also like