Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Non primitive data types

1. String:
//Declare String without using new operator
String s = "GeeksforGeeks";

// Declare String using new operator


String s1 = new String("GeeksforGeeks");

2. Class : A class is a user-defined blueprint or prototype from which


objects are created. It represents the set of properties or methods that
are common to all objects of one type.
3. Object: It is a basic unit of Object-Oriented Programming and
represents the real-life entities. A Java program can create many
objects. 'object' and 'instance' are the same thing, but the word
'instance' indicates the relationship of an object to its class.

4. Interface: Like class, interface also has methods and variables, but
the methods declared in an interface are by default abstract (only
method signature, no body).
5. Array :
 All arrays are dynamically allocated. Dynamically allocating an array means we can
choose an array length at runtime.
 Arrays are objects in Java, we find their length using member length.
 The size of an array must be specified by an int value and not long or short.

 The direct superclass of an array type is Object. (Object class is present in java.lang
package. Every class in Java is directly or indirectly derived from the Object class.). The
Object class is the parent class of all the classes in java by default. In other words, it is the
topmost class of java.
Example : to declare an array
int intArray[];
or int[] intArray;
Static keyword
The static keyword in Java is used for memory management mainly.
The static can be:

Variable (also known as a class variable)


Method (also known as a class method)
Block
Nested class
Variables in Java

 local variable ( automatic): A variable declared inside the body of the method. A local variable cannot be

defined with "static" keyword.

 instance variable ( non static ): A variable declared inside the class but they are outside the body of the

method and constructor . If they are non-static variables, every instance of that class (object) has it's own

copy of that variable. Changes made to the variable don't reflect in other instances of that class. They are

created whenever an object of the class is instantiated.

 static variable ( class variables): We can create a single copy of static variable and share among all the
instances ( objects) of the class. Memory allocation for static variable happens only once when the class is
loaded in the memory. They are declared outside method. They can be accessed by class name. There's only
one copy of that variable that is shared with all instances of that class. If changes are made to that variable,
all other instances will see the effect of the changes. Advantages of static variable is that It makes your
Static variables

 When a variable is declared as static, then a single copy of the variable is


created and shared among all objects at a class level.

 Static variables are, essentially, global variables.

 All instances of the class share the same static variable.

 We can create static variables at class level only.

 static block and static variables are executed in the order they are present
in a program.
Static methods (Rules)

1. Static Methods do not need an object to access static variables


2. Non static methods and variables need an object to access static variables.
3. A static method can access only static variables of class and invoke only
static methods of the class, they do not need objects.
4. Non static methods can access static variables and methods.
5. VVimp : Static methods can be accessed directly in static and non-static
methods.
6. The static method can not use non static data member or call non-static
method directly.
7. this and super cannot be used in static context.
Java program to demonstrate execution
of static blocks and variables
class Test { public static void main(String[] args)
static int a = m1(); {
static System.out.println("Value of a : " + a);
{ System.out.println("from main");
System.out.println("Inside static block"); } // rule 1
} }

static int m1()


o/p
{
from m1
System.out.println("from m1");
Inside static block
return 20;
Value of a : 20
}
from main
Java static block class A2
• Is used to initialize the static {
data member. static
{ System.out.println("static block is
• It is executed before the main invoked");
method at the time of }
classloading public static void main(String args[])
{
System.out.println("Hello main");
}
}
class Student{ void display()
int rollno; { System.out.println(rollno+" "+name+" "+college);
String name; } // rule 5
static String college = "ITS"; public class TestStaticMethod
//static method to change the value of static
variable {
static void change() public static void main(String args[])
{ {
college = "BBDIT"; Student.change(); // rule 3 & rule 1
} Student s1 = new Student(111,"Karan");
//constructor to initialize the variable Student s2 = new Student(222,"Aryan");
Student(int r, String n)
s1.display();
{
s2.display();
rollno = r;
}
name = n;
} }
o/p
111 Karan BBDIT
222 Aryan BBDIT
E x am p le

class Student{ public class TestStaticVariable1


int rollno; //instance variable {
String name;
public static void main(String args[])
static String college ="ITS"; //static variable
{
//constructor
Student(int r, String n, String m) Student s1 = new Student(111,"Karan“, “LBS”);
{ Student s2 = new Student(222,"Aryan“, “KBS”);
rollno = r; s1.display(); s2.display();
name = n;
Student.college="BBDIT"; // rule 1
college=m;
s1.display();
}
//method to display the values s2.display();
void display () }
{ }
System.out.println(rollno+" "+name+" "+college);
}
}
111 Karan KBS
222 Aryan KBS 111 Karan BBDIT
Example 1 Example 2
class JavaExample{ class JavaExample
static int i = 10; {
static int i = 100;
static String s = "Beginnersbook";
static String s = "Beginnersbook";
//This is a static method //Static method
public static void main(String static void display()
args[]) {
System.out.println("i:"+i);
{
System.out.println("i:"+s); //rule 3
System.out.println("i:"+i); // }
rule 3 //non-static method
System.out.println("s:"+s); void funcn()
{ //Static method called in non-static method
}
display(); // rule 4
} }
public static void main(String args[])
{ o/p:
JavaExample obj = new JavaExample();
i: 10
//You need to have object to
call this non-static method i:Beginnersbook
obj.funcn();
//Static method called in another i: 10
static method i:Beginnersbook
display(); // rule 3
}
}
class A
{
int a=40;

public static void main(String args[])


{
System.out.println(a); // rule 6
}
}
o/p
• cte
Why is the Java main method static?

Ans) It is because the object is not required to call a static method. If it

were a non-static method, JVM creates an object first then call main()

method that will lead the problem of extra memory allocation.


static classes

• The class in which the nested class is defined is known as the Outer

Class.

• Unlike top-level classes, Inner classes can be Static. Non-static nested

classes are also known as Inner classes.


• An instance of an inner class cannot be created without an instance of the

outer class.

• Therefore, an inner class instance can access all of the members of its

outer class, without using a reference to the outer class instance.

• For this reason, inner classes can help make programs simple and concise.
Static Class
1. A class can be made static only
if it is a nested class.
2. Nested inner static class doesn’t
need reference of the Outer
class.
3. A static class cannot access
non-static members of the
Outer class.
4. Inner classes can access both
static and non-static members of
the outer class.
class JavaExample public static void main(String args[])
{ { JavaExample.MyNestedClass obj = new
JavaExample.MyNestedClass();
private static String str = "BeginnersBook";

private int l= 10;


obj.disp(); // we need object to access
//Static inner class non static method of inner class
static class MyNestedClass S.o,p (“My String :” +MyNestedClass.str);
{ //non-static method
S.o.p( “ My number:”+MyNestedClass.l);//rule
3 or cte
public void disp()
}
{ System.out.println(str);
}
} } o/p: Beginner’s Book
}
Final variable, method and class
class Bike9{ class Bike
{
final int speedlimit=90; //final variable final void run()
void run() { System. out. println("running");
}
{ }
speedlimit=400; If you make any method as final, you cannot override it.
class Honda extends Bike
} {
public static void main(String args[]){ void run()

Bike9 obj=new Bike9(); { System.out.println("running safely with 100kmph");


}
obj.run();
} public static void main(String args[]){
Honda honda= new Honda();
} honda.run();
}

Output: compile time error }


Output: cte
If you make any class as final, you cannot extend it.
Final method is inherited but you cannot override it.
class Bike
final class Bike { }
{
final void run()
class Honda1 extends Bike{
{
void run()
System.out.println("running...");
{System.out.println("running safely with 100kmph");
}
}
}
class Honda2 extends Bike
public static void main(String args[])
{
{
public static void main(String args[]){
Honda1 honda= new Honda1();
new Honda2().run();
honda.run(); }
} }
}

Output: CTE o/p: running…..

You might also like