Java Unit - 1 Notes
Java Unit - 1 Notes
Syllabus
Unit – I
UNIT - 1 13
Objects and Classes: Basics of objects and classes in java, Methods
and objects, Instance of operator, Visibility modifiers, Method
Overloading, Constructors, Static Members
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 1
Object Oriented with java programming
Introduction to Java:
What is java?
Java is a high-level, object-oriented programming language developed by Sun
Microsystems (now owned by Oracle Corporation).It was first released in 1995 and
has since become one of the most popular programming languages in the world,
known for its platform independence, versatility, and wide range of applications.
Java was developed by a team of engineers led by James Gosling at Sun Microsystems
in the early 1990s. The project was initially called "Oak" but was later renamed "Java"
to avoid trademark issues. The official release of Java 1.0 occurred in 1995. Over the
years, Java has evolved with contributions from various individuals and
organizations, and it is now maintained by Oracle Corporation, which acquired Sun
Microsystems in 2010. James Gosling is often credited as the "father of Java" for his
key role in its creation.
3 billion mobile phones, 125 million TV sets, and 1 billion computers run Java
programming language.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 2
Object Oriented with java programming
1. "Write once, run anywhere" philosophy, meaning that Java programs can be
compiled into byte code, which can then be executed on any platform that has a
Java Virtual Machine (JVM) installed.
3. Object-Oriented: Java organizes code into objects, which are like building blocks
that represent real-world things. This makes it easier to manage and reuse code, like
Lego pieces fitting together.
5. Safe and Secure: Java has built-in features to protect your code and data from
security threats, like viruses or hackers. It's like having a security guard watching over
your program.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 3
Object Oriented with java programming
6. Robust and Reliable: Java is designed to be strong and dependable, with features
that help prevent crashes and errors. It's like having a sturdy shield that protects your
program from breaking.
7. Lots of Tools and Libraries: Java comes with a lot of helpful tools and pre-made
code (libraries) that you can use to build your programs faster. It's like having a
toolbox full of handy gadgets for your project.
8. Fast Performance: While Java may not be the fastest language, it's still pretty speedy,
especially with modern improvements. It's like a car that may not be a race car, but
it still gets you where you need to go efficiently.
9. Works with Multiple Tasks: Java can handle doing multiple things at once
(multithreading), which is useful for tasks like downloading files while playing music.
It's like being able to cook dinner while chatting with a friend on the phone.
10. Interpreted:
1. Compilation: When you write Java code, you first compile it using the Java compiler
(javac). The compiler translates your human-readable Java code into an intermediate
form called bytecode. Bytecode is not machine code but rather a set of instructions that
can be executed by the Java Virtual Machine (JVM).
2. Interpretation: The JVM then interprets the bytecode and executes it on the target
platform. The JVM is responsible for translating bytecode instructions into machine
code that the underlying hardware can understand. This process happens dynamically at
runtime.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 4
Object Oriented with java programming
1. Object :
It is a basic unit of Object Oriented Programming and represents the real life entities.
An object consists of :
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. In general, class declarations can include these components, in order:
Class is “blueprint”
3. Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviours of a parent object. It is an important part of OOPs (Object
Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes.
5. Abstraction
Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details.
Ex: A car is viewed as a car rather than its individual components.
6. Encapsulations
Encapsulation is defined as the wrapping up of data under a single unit. It is the
mechanism that binds together code and the data it manipulates.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 5
Object Oriented with java programming
Documentation Section:
This typically refers to the comments and documentation within a Java file that
describe the purpose, usage, and behavior of the code. It includes Javadoc comments
which are used to generate documentation. comments are: single line comment (//)
multiline comments(/* */)
Package Declaration:
Every Java class belongs to a package, which is declared at the beginning of the file.
Packages help organize classes into meaningful groups and prevent naming conflicts.
Import Statements:
Java allows classes to import other classes or entire packages to use their
functionalities without fully qualifying their names. Import statements are used to
include these external classes or packages.
Interface Section:
In Java, an interface is a reference type that defines a set of abstract methods. It
specifies a contract that implementing classes must adhere to, without providing any
implementation details.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 6
Object Oriented with java programming
Class Definition:
In Java, a class is a blueprint for creating objects. It encapsulates data (in the form of
fields or variables) and behavior (in the form of methods or functions) into a single
unit.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 7
Object Oriented with java programming
Data types specify the different sizes and values that can be stored in the variable.
Every variable declared before using it in program.
Java programming language has a rich set of data types.
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays and etc.
Class
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 8
Object Oriented with java programming
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 9
Object Oriented with java programming
float f = 10.5f;
double d = 20.55;
char c = 'A';
boolean flag = true;
[Link](b);
[Link](s);
[Link](i);
[Link](l);
[Link](f);
[Link](d);
[Link](c);
[Link](flag);
}
}
OUTPUT:
10
100
1000
10000
10.5
20.55
A
true
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 10
Object Oriented with java programming
Java Variables
In Java, a variable is a name given to a memory location where data is stored.
The value of a variable can change while the program runs.
Think of a variable as a labeled box in memory that holds some value.
Example:
int age = 20;
Here:
int is the data type
age is the variable name
20 is the value stored in memory
Java mainly has three types of variables based on where they are declared and how memory
is allocated.
1. Local Variable.
2. Instance Variable.
3. Static Variable.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 11
Object Oriented with java programming
1. Local Variable
What it is local variable?
A local variable is declared inside a method, constructor, or block.
It exists only while that method is running.
How it works
When a method is called:
Java creates a stack frame for that method.
It creates Temporary memory.
Local variables are stored inside this stack frame.
Automatically destroyed after method execution
Key points
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 12
Object Oriented with java programming
2) Instance Variable
Example:
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 13
Object Oriented with java programming
3) Static variable
What it is Static variable?
A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances of
the class. Memory allocation for static variables happens only once when the class is
loaded in the memory.
How it works
Key points
Think of static variables like a notice board. Everyone reads the same information.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 14
Object Oriented with java programming
Lab Program 6: Program with class variable that is available for all instances of a class.
Use static variable declaration. Observe the changes that occurring the object’s
member variable values.
OUTPUT:
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 15
Object Oriented with java programming
10
10.0
Java Variable Example: Narrowing (Typecasting)
public class Simple
{
public static void main(String[] args)
{
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
[Link](f);
[Link](a);
}
}
Output:
10.5
10
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 16
Object Oriented with java programming
Java Operators:
Operators are symbols that perform operations on variables and values. For
example, + is an operator used for addition, while * is also an operator used for
multiplication.
Operators in Java can be classified into:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators
Arithmetic operators are used to perform arithmetic operations on variables and data.
For example, a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various
other arithmetic operators in Java.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 17
Object Oriented with java programming
Relational operators are used to check the relationship between two operands.
For example : a < b; // check if a is less than b
Here, < operator is the relational operator. It checks if a is less than b or not. It
returns either true or false.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 18
Object Oriented with java programming
Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).
The various bitwise operators present in Java are:
Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 19
Object Oriented with java programming
PROGRAM ON OPERATORS
class OperatorsDemo
{
public static void main(String[] args)
{
// --------- Arithmetic Operators ---------
int a = 12, b = 5;
[Link]("Arithmetic Operators:");
[Link]("a + b = " + (a + b));
[Link]("a - b = " + (a - b));
[Link]("a * b = " + (a * b));
[Link]("a / b = " + (a / b));
[Link]("a % b = " + (a % b));
num += 5;
[Link]("num += 5 : " + num);
num -= 3;
[Link]("num -= 3 : " + num);
num *= 2;
[Link]("num *= 2 : " + num);
num /= 4;
[Link]("num /= 4 : " + num);
num %= 3;
[Link]("num %= 3 : " + num);
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 20
Object Oriented with java programming
A control structure decides how the program runs, meaning which statements run,
how many times they run, and in what order.
By default, Java runs code line by line from top to bottom.
Control structures change this normal flow.
if
if-else
if-else-if
nested if
switch
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 21
Object Oriented with java programming
expression (returns
either true or false).
If the expression is evaluated to true,
if (number > 0)
{
[Link]("Number is positive.");
}
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 22
Object Oriented with java programming
How it works
1. The condition is
evaluated.
2. If the condition is true
→ code inside the if
block runs.
3. If the condition is false
→ code inside the else
block runs.
4. Only one block executes
at a time.
Example:
OUTPUT
class CheckNumber
{ Number is positive.
public static void main(String[] args) This statement is
{ always executed.
int number = 10;
if (number > 0)
{
[Link]("Number is positive.");
}
else
{
[Link]("Number is not positive.");
}
[Link]("This statement is always executed.");
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 23
Object Oriented with java programming
5.
Example:
class IfElseLadderExample OUTPUT
{ Grade B
public static void main(String[] args)
{
int marks = 78;
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 24
Object Oriented with java programming
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 25
Object Oriented with java programming
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 26
Object Oriented with java programming
class SwitchExample
{
public static void main(String[] args)
{
int day = 3;
switch (day)
{
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
default:
[Link]("Invalid day");
}
}
}
Output:
Wednesday
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 27
Object Oriented with java programming
Java Looping:
In computer programming, loops are used to repeat a block of code.
For example, if you want to show a message 100 times, then rather than typing the same
code 100 times, you can use a loop.
1. for loop
2. while loop
3. do...while loop
[Link] loop:
Java for loop is used to run a block of code for a certain number of times. The syntax
of for loop is:
Here,
1. The initialization initializes and/or declares variables and executes only once.
2. The condition is evaluated. If the condition is true, the body of the for loop is
executed.
3. The Increment/Decrement updates the value of initialization.
4. The condition is evaluated again. The process continues until the condition is false.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 28
Object Oriented with java programming
// for loop
for (int i = 1; i <= n; i++)
{
sum = sum + i
}
[Link]("Sum = " + sum);
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 29
Object Oriented with java programming
//Intialization
while (Condition)
{
// body of loop
//Increment /Decrement
}
Here,
1. A while loop evaluates the Condition inside the parenthesis ().
2. If the Condition evaluates to true, the code inside the while loop is executed.
3. The Condition is evaluated again.
4. This process continues until the Condition is false.
5. When the Condition evaluates to false, the loop stops.
while ()
{
[Link]("Enter password: ");
password = [Link]();
}
[Link]("Access granted");
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 30
Object Oriented with java programming
do
{
// body of loop
//Increment/Decrement
} while(Condition);
Here,
1. The body of the loop is executed at first. Then the Condition is evaluated.
2. If the Condition evaluates to true, the body of the loop inside the do statement is
executed again.
3. The Condition is evaluated once again.
4. If the Condition evaluates to true, the body of the loop inside the do statement is
executed again.
5. This process continues until the Condition evaluates to false. Then the loop stops.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 31
Object Oriented with java programming
// outer loop
for (int i = 1; i <= 5; ++i)
{
// codes
// inner loop
for(int j = 1; j <=2; ++j)
{
// codes
}
..
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 32
Object Oriented with java programming
It works on the basis of elements and not the index. It returns element one by one in
the defined variable.
Syntax:
Here,
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 33
Object Oriented with java programming
1. Break statement
1. Using Break Statement to exit a loop:
In java, the break statement is used to terminate the execution of the nearest looping
statement or switch statement. The break statement is widely used with the switch
statement, for loop, while loop, do-while loop.
Syntax:
break;
When a break statement is found inside a loop, the loop is terminated, and the control
reaches the statement that follows the loop. here is an example:
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 34
Object Oriented with java programming
2. Java continue:
While working with loops, sometimes you might want to skip some statements or
terminate the loop. In such cases, break and continue statements are used.
The continue statement skips the current iteration of a loop (for, while, do...while, etc).
After the continue statement, the program moves to the end of the loop. And, test
expression is evaluated (update statement is evaluated in case of the for loop).
continue;
OUTPUT:
import [Link].*; 0
class ContinueExample 1
{ 2
public static void main(String[] args) 3
{ 4
for (int i = 0; i < 10; i++) 5
{
if (i == 6) 7
{ 8
[Link](); 9
continue;
}
[Link](i);
}
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 35
Object Oriented with java programming
3. Return Statement
The “return” keyword can help you transfer control from one method to the method
that called it. Since the control jumps from one part of the program to another, the
return is also a jump statement.
Syntax:
return value;
return sum;
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 36
Object Oriented with java programming
Java Array:
What is an Array in Java?
An array refers to a data structure that contains homogeneous elements. This means
that all the elements in the array are of the same data type. Let's take an example:
This is an array of seven elements. All the elements are integers and homogeneous. The box
below the array is called the index, which always starts from zero and goes up to n-1
elements.
Elements stored under a single name: All the elements are stored under one name. This name
is used any time we use an array.
Advantages:
Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
Random access: We can get any data located at an index position.
Java arrays enable you to access any element randomly with the help of indexes
It is easy to store and manipulate large data sets
Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its
size at runtime. To solve this problem, collection framework is used in Java which
grows automatically.
Java cannot store heterogeneous data. It can only store a single type of primitives
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 37
Object Oriented with java programming
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 38
Object Oriented with java programming
1. Declare the Array: Specify the type of elements it will store and the name of the array.
2. Instantiate the Array: Use the `new` keyword to allocate memory for the array.
Example:
Let's create an array of integers to store 5 numbers.
# Step 1: Declare the Array
Ex: int[ ] numbers;
This line declares an array named `numbers` that will hold integers.
# Step 2: Instantiate the Array
Ex: numbers = new int[5];
This line allocates memory for 5 integers. Now `numbers` can hold 5 elements.
# Step 3: Initialize the Array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 39
Object Oriented with java programming
Here, we assign values to each element in the array using their indices.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 40
Object Oriented with java programming
Declaration Syntax
You can declare a 2D array in these ways:
dataType[][] arrayName;
dataType arrayName[][];
Example:
int[][] arr;
Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional
array.
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
[Link](arr[i][j]+" ");
}
[Link]();
}
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 41
Object Oriented with java programming
A class contains:
1. Field/Data Member: Data items are called fields or data members (Instance of class)
2. Method : Functions are called methods.
3. Constructor
4. Block
In java variables are known as instances of classes, which are actual objects.
Syntax:
class ClassName
{
// fields
// methods
}
In a class:
Example:
class Student
{
int id; // field
String name; // field
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 42
Object Oriented with java programming
Field Declaration:
Data is encapsulated in a class by placing data fields inside the body of a class definition.
These variables are called instance variables because of they are created whenever an object
of the class they are instantiated.
Here, in above example for class field declaration Rectangle is the class name and in this class
we declared the two variable like length and width.
Note: If we are created a class like above example, no storage space or memory allocation
has been created for these variables, also called as member variables.
Method Declaration:
A method is a collection of statements that perform some specific task and return the result to
the caller. A method can perform some specific task without returning anything.
Syntax
modifier data_type method_name (Parameter List)
{
// method body
}
Example1:
class Rectangle
{
int length;
int width;
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 43
Object Oriented with java programming
Java Object :
Object Definitions:
Creation of Object:
An object in java is essentially a block of memory that contains space to store all the
instance variables. Creating an object is also referred to as instantiating an object.
Objects in java created using new operator. The new operator dynamically allocates
memory for an object.
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 44
Object Oriented with java programming
The method Student( ) is the default constructor of the class student. We can create any
number of objects of Student.
Example:
void display()
{
[Link]("Roll No: " + rollNo);
[Link]("Name: " + name);
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 45
Object Oriented with java programming
void area()
{
double result = pi * radius * radius;
[Link]("Area of Circle: " + result);
}
Name: Raju
void readName()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter name: ");
name = [Link]();
}
void displayName()
{
[Link]("Name: " + name);
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 46
Object Oriented with java programming
We can’t use instance variables outside the class and also methods. To do this we must
concerned object and the dot(.) operator as shown below.
Syntax:
Object_name.variable_name=value;
Object_name.method_name(parameter list);
Where,
Object_name Name of the object
variable_nameName of the variable.
Method_nameMethod
Parameter_listActual values to the parameter.
[Link]=1114520;
[Link]="Shridhar";
[Link]=24;
[Link]("Roll No = "+[Link]);
[Link]("Name = "+[Link]);
[Link]("Age = "+[Link]);
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 47
Object Oriented with java programming
Instance Operator:
In Java, the instance operator is represented by the keyword “instanceof ”. It is used to
check whether an object is an instance of a particular class or implements a particular
interface.
This operator returns `true` if the object is an instance of the specified class or implements
the specified interface, otherwise it returns `false`.
For example:
OUTPUT
class Dog Dog is barking
{ True : The animal is a Dog.
void bark()
{
[Link](“Dog is barking”);
}
}
public class Animal extends Dog
{
public static void main(String[] args)
{
Dog d = new Dog();
[Link]();
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 48
Object Oriented with java programming
The access modifiers in Java specifies the accessibility or scope of a field, method,
constructor, or class.
We can change the access level of fields, constructors, methods, and class by applying
the access modifier on it.
1. Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
Example(Click Here)
2. Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
Example(Click Here)
3. Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.
Example(Click Here)
4. Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
Example(Click Here)
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 49
Object Oriented with java programming
In Java, if a class having two or more methods with having the same name but with
differ in parameters lists. These methods are called overloaded methods and this is
called method overloading.
For example:
class MethodOverload
{
void func( ) { ... }
void func(int a) { ... }
float func(double a) { ... }
float func(int a, float b) { ... }
}
Here, the func( ) method is overloaded. These methods have the same name but accept
different arguments.
class Adder
{
static int add(int a,int b) OUTPUT
{ 30
return a+b;
}
60
static int add(int a,int b,int c)
{
return a+b+c;
}
}
class TestOverloading1
{
public static void main(String[] args)
{
[Link]([Link](10,20));
[Link]([Link](10,20,30));
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 50
Object Oriented with java programming
In this example, we have created two methods that differs in data type. The first add
method receives two integer arguments and second add method receives two double
arguments.
class Addition
{ OUTPUT
int addInt() Default integer sum: 30
{ Integer sum: 15
return 10 + 20; Default Float sum: 5.0
Float sum: 6.0
}
float addFloat()
{
return 2.5f+2.5f;
}
float addFloat(float a, float b)
{
return a + b;
}
}
public class OverloadingExample
{
public static void main(String[] args)
{
Addition obj = new Addition();
[Link]("Default integer sum: " + [Link]());
[Link]("Integer sum: " + [Link](5, 10));
[Link]("Default Float sum: " + [Link]());
[Link]("Float sum: " + [Link](2.5f, 3.5f));
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 51
Object Oriented with java programming
Constructor:
A constructor in Java is a special method that is used to initialize objects. The constructor
is called when an object of a class is created.
At the time of calling constructor, memory for the object is allocated in the memory.
It calls a default constructor if there is no constructor available in the class. In such case,
Java compiler provides a default constructor by default.
<class_name> ( )
{
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 52
Object Oriented with java programming
2. Parameterized Constructor:
A parameterized constructor in Java is a constructor that accepts parameters, allowing you to
initialize an object with specific values at the time of its creation.
Syntax:
public class ClassName
{
// Instance variables
datatype variableName1;
datatype variableName2;
// Parameterized constructor
public ClassName(data_type1 parameterName1, data_type2 parameterName2, ...)
{
// Initialization of instance variables with parameter values
// Parameterized constructor body.
}
}
Here is an example:
[Link]();
[Link]();
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 53
Object Oriented with java programming
Constructor Overloading:
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 54
Object Oriented with java programming
Static Members:
The static keyword in Java is used for memory management mainly. We can apply
static keyword with variables, methods, block.
The static keyword belongs to the class than an instance of the class.
1. Static Variable:
If you declare any variable as static, it is known as a static variable.
The static variable can be used to refer to the common property of all objects (which
is not unique for each object), for example, the company name of employees, college
name of students, etc.
The static variable gets memory only once in the class area at the time of class
loading.
OUTPUT:
class Student
{
int rollno;
111 Karan ITS
String name; 222 Aryan ITS
static String college ="Tungal jamakhandi";
Student(int r, String n)
{
rollno = r;
name = n;
}
void display( )
{
[Link](rollno+" "+name+" "+college);
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 55
Object Oriented with java programming
2. Static Method:
If you apply static keyword with any method, it is known as static method.
o A static method belongs to the class rather than the object of a class.
o A static method can be invoked without the need for creating an instance of a class.
o A static method can access static data member and can change the value of it.
3. Static block:
Here is an Example:
class StaticBlock
OUTPUT:
{
static Static block is invoked
{
[Link]("static block is invoked"); Hello JAVA
}
public static void main(String args[ ])
{
[Link]("Hello JAVA");
}
}
Prof. Raju Vathari Tungal BCA and BSc Degree College-Jamkhandi Page 56