[go: up one dir, main page]

0% found this document useful (0 votes)
3 views31 pages

Core Java Day 4

The document covers core concepts of Java, focusing on non-static members, variables, methods, and initializers, as well as constructors, including their types and purposes. It explains the loading process of objects and the significance of the 'this' keyword in accessing current object references. Additionally, it discusses constructor overloading and chaining, detailing how to initialize object attributes through various constructor forms.

Uploaded by

tidketanmay5
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)
3 views31 pages

Core Java Day 4

The document covers core concepts of Java, focusing on non-static members, variables, methods, and initializers, as well as constructors, including their types and purposes. It explains the loading process of objects and the significance of the 'this' keyword in accessing current object references. Additionally, it discusses constructor overloading and chaining, detailing how to initialize object attributes through various constructor forms.

Uploaded by

tidketanmay5
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
You are on page 1/ 31

ALPHA

CORE JAVA - SESSION 12


INDEX
● Non static and non static members
● Non-static variables
● Non-static method
CORE JAVA ● Non static context
SESSION 12 ● Non-static initializers
NON STATIC AND NON STATIC MEMBERS

NON STATIC :
● Any member declared in a class and not prefixed with a static modifier is known as a
non-static member of a class.
● Non-static members belong to an instance of a class. Hence it is also known as an
instance member or object member.
● The memory for the non-static variable is allocated inside the heap area(instance of a
class).
● We can create any number of instances for a class.
● Non-static members will be allocated in every instance of a class.
NON STATIC MEMBERS :
● Non static variable
● Non static method
● Non static initializers
● Constructors
NON STATIC VARIABLE

NON STATIC VARIABLE :


A variable declared inside a class block and not prefixed with a static modifier is
known as a non-static variable.
CHARACTERISTICS :
● We can’t use the non-static variable without creating an object.
● We can only use the non-static variable with the help of object reference.
● Non-static variables are assigned with default during the object loading process.
● Multiple copies of non-static variables will be created (once for every object).
NON STATIC METHOD

NON STATIC METHOD :


A method declared in a class block and not prefixed with a static modifier is
known as a non-static method.
CHARACTERISTICS :
● A method block will get stored inside the method area and a reference of the method
is stored inside the instance of a class [object].
● We can’t call the non-static method of a class without creating an instance of a
class[object].
● We can’t access the non-static members directly with the help of class names.
● Non-static members can’t be accessed directly with their names inside the static
context.
NON STATIC CONTEXT

NON STATIC CONTEXT :


● The block which belongs to the non static method is known as non static context.
● Inside a non static context we can use static and non static members of the same
class directly by using its name.
NON STATIC INITIALIZERS

NON STATIC INITIALIZERS :


● Non-static initializers will execute during the loading process of an object.
● Non-static initializers will execute once for every instance of a class created.
[object created].
PURPOSE OF NON STATIC INITIALIZERS :
Non-static initializers are used to execute the startup instructions for an object.
TYPES OF NON STATIC INITIALIZERS :
1. Single line non static initializer
2. Multi line non static initializer
1. SINGLE LINE NON STATIC INITIALIZER :
Syntax to create single line non static initializers :
datatype variable = value / reference
1. MULTI LINE NON STATIC INITIALIZER :
Syntax to create multi line non static initializers :
{
// statements ;
}
NOTE :
All the Non-static initializers will execute from top to bottom order for
every object creation.
THIS KEYWORD

this :
● It is a keyword.
● It is a non static variable it holds the reference of current executing object.
USES OF THIS :
● Used to access the members of current object.
● It is used to give the reference of the current object.
● Reference of a current object can be passed from the method using ‘this’
keyword.
● Calling a constructor of the same class is achieved with the help of this call
statement.
SESSION-1 ACTIVITY
1. Explain how the loading process of the class Fruit takes place based on Java
Runtime Memory.
class Fruit
{
String name;
public void printName()
{
System.out.println(name);
}
public static void main(String[] args)
{
Fruit f1 = new Fruit();
Fruit f2 = new Fruit();
f1.name = “mango”;
f2.name = “apple”;
f1.printName();
f2.printName();
}
}
SESSION-1 ACTIVITY

2. Design a blueprint for the following object.


Employee
eid
name
salary attributes

toSet Attributes()
actions/behaviours
toPrintAttributes()

Create a class to store details of at least 3 employees and display their


attributes?
ALPHA
CORE JAVA - SESSION 13
INDEX
● Constructor
● Syntax to create constructor
● Constructor body
● Purpose of the constructor
● Classification of constructor
CORE JAVA ● No argument constructor
SESSION 13 ● Loading process of an object
CONSTRUCTOR

CONSTRUCTOR :
Constructor is a special type of non-static method whose name is the same as the
class name but it does not have a return type.
Syntax to create the constructor :
A programmer can define a constructor by using the following syntax :
[access_modifier] [modifier] className([Formal_Arguments])
{
// initialization ;
}
CONSTRUCTOR BODY

CONSTRUCTOR BODY :
A constructor body will have the following things :
● Load instructions added by the compiler during compile time.
● Non static initializers of the class.
● Programmer written instructions.
PURPOSE OF THE CONSTRUCTOR

PURPOSE OF THE CONSTRUCTOR :


During the execution of the constructor,
● Non-static members of the class will be loaded into the object.
● If there is a non-static initializer in the class they start executing from top to bottom
order.
● Programmer written instruction of the constructor gets executed.
NOTE :
If the programmer fails to create a constructor then the compiler will add a
default constructor.
CLASSIFICATION OF CONSTRUCTOR

CLASSIFICATION OF CONSTRUCTOR :
Constructors can be classified into two types based on hormonal argument,
1. No argument constructor
2. Parameterized constructor
NO ARGUMENT CONSTRUCTOR :
A constructor which doesn’t have a formal argument is known as no argument
constructor.
PARAMETERIZED CONSTRUCTOR :
A constructor which have a formal argument is known as parameterized
constructor.
NO ARGUMENT CONSTRUCTOR

NO ARGUMENT CONSTRUCTOR :
A constructor which doesn't have a formal argument is known as a no-argument
constructor.
Syntax to create no argument constructor :
[access modifier] [modifier] className()
{
//code ;
}
NOTE :
If the programmer fails to create a constructor then compiler implicitly add
a no- argument constructor only.
LOADING PROCESS OF AN OBJECT

LOADING PROCESS OF AN OBJECT :


● a new keyword will create a block of memory in a heap area
● Constructor is called.
● During the execution of the constructor,
I. All the non-static members of the class are loaded into the object.
II. If there are non-static initializers they are executed from top to bottom
order.
III. Programmer written instruction of the constructors will be executed.
● The execution of the constructor is completed.
● The object is created successfully.
● The reference of an object is returned by the new keyword.
● These steps are repeated for every object creation.
EXAMPLE :

class School
{
String schoolName = “ABC HSC”;
{
S.o.pln(“School name is “+ schoolName);
}
public void test()
{
S.o.pln(“From test”);
}
String sname;
Int sid;
public static void main(String []args)
{
School s = new School();
}
}
Once execution Thus theisloading
of constructor Constructor
process
completed ofisan
called
the reference object is is returned back to the reference variable
of an object
New keyword will create a block of memory in a heap
completed
area

ox1

1.Load non static members


2. Execute non static initializers test() 0x2
3.Execute the Programmer
written instructions

Frame 1 schoolName ABC


null HSC

s OX1 sname null

sid 0

Frame 0

STACK AREA HEAP AREA


SESSION-2 ACTIVITY
1. Explain how the loading process of the class Pen takes place
based on Java Runtime Memory.

class Pen
{
double price;
{
System.out.println("class pen");
price=10;
}
Pen()
{
System.out.println("pen()");
price = 50;
}

public static void main(String[] args)


{
Pen e = new Pen();
System.out.println(e.price);
}
}
SESSION-2 ACTIVITY
Example Program 1
Step 1: Create a class 'Demo'.
Step 2: Declare Non-static field/variable
Step 3: Create a No-arg constructor and initialize the
Variable
Step 4: Create a class ‘Demo2’ and create object for Demo class, print
the value in the variable.
Step 5: explain the loading process.
ALPHA
CORE JAVA - SESSION 14
INDEX
● parameterized constructor
● purpose of parameterized constructor
● constructor overloading
CORE JAVA
● constructor chaining
SESSION 14
● this() statement
PARAMETERIZED CONSTRUCTOR

PARAMETERIZED CONSTRUCTOR :
The constructor which has a formal argument is known as parameterized
constructor.
PURPOSE OF THE PARAMETERIZED CONSTRUCTOR :
Parameterized constructors are used to initialize the variables (non-static) by
accepting the data from the constructor in the object creation statement.
CONSTRUCTOR OVERLOADING

CONSTRUCTOR OVERLOADING :
If a class is having more than one constructor it is known as constructor overloading.
RULE :
The signature of the constructor must be different.
CONSTRUCTOR CHAINING

CONSTRUCTOR CHAINING :
● A constructor calling another constructor is known as constructor chaining.
● In java, we can achieve constructor chaining by using two ways
1. this() (this call statement)
2. super() (super call statement)
this() statement

this() :
It is used to call the constructor of the same class from another constructor.
RULE :
● this() can be used only inside the constructor.
● It should always be the first statement in the constructor.
● The recursive call to the constructor is not allowed (Calling by itself).
● If a class has n constructors we can use this statement in n-1 constructors only(at
least a constructor should be without this()
NOTE :
If the compiler has this() statement then the compiler doesn’t add load instruction
& non-static initializers into the constructor body.
SESSION-3 ACTIVITY
1. Explain how the loading process of the class Fruit takes place
based on Java Runtime Memory.
class Pen
{
double price = 80;
{
System.out.println(“hello world”);
}
Pen()
{

System.out.println(“no arg constructor”);


}
Pen(double p)
{

System.out.println(“parameterized constructor”);
price=p;
}
}
class Driver
{
Public static void main (String[] args)
{
Pen a = new Pen(20);
System.out.println(a.price);
}
}
SESSION-3 ACTIVITY
Design A class for the following object
Student

student_id
name
attributes
age
percentage

display_Attributes() actions/behaviours
edit_name()

The service user must be able to create a student object as follows


● Without passing any data.
● By initializing only student_id.
● By initializing only student_id and name.
● By initializing only student_id, name and age.
● By initializing all the attributes.

You might also like