0% found this document useful (0 votes)
11 views

82 - Oopexp1

The document describes creating a class with instance variables and methods in Java. It defines instance variables, their default values, and instance methods. It provides an example class with an instance variable size, method bark() to print different sounds based on size, and a test class to create objects and call the bark() method.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

82 - Oopexp1

The document describes creating a class with instance variables and methods in Java. It defines instance variables, their default values, and instance methods. It provides an example class with an instance variable size, method bark() to print different sounds based on size, and a test class to create objects and call the bark() method.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ATHARVA EDUCATIONAL TRUST'S

ATHARVA COLLEGE OF ENGINEERING


(Approved by AICTE, recognized by Government of Maharashtra
& Affiliated to University of Mumbai)
Department of Computer Engineering Academic Year 2020-21

Experiment No. 1 : Illustrate to create a class with member and


methods by accessing the instance variables and
instance methods using dot (.) operator and
display.

Aim : Illustrate to create a class with member and methods by accessing the
instance variables and instance methods using dot (.) operator and
display.

Theory :

Instance Variable in Java

In any programming language, the program needs identifiers for storing


different values that can be used throughout the program. These identifiers are
variables.

Variable in Java
o A variable is a name assigned to a value that is stored inside the system
memory. The value can be updated during the program execution.
o In Java programming, the variables used for the program need to declare
them first.
o The variable is declared using a data type followed by the identifier
name. The variable can be initialized at the time of declaration or it can
be assigned a value taken from the user during the program execution.
o There are basically three types of variables in Java,
1. Java Local variable
2. Java Instance variable
3. Java Static variable / Java class variable

Java Instance Variable


o The variables that are declared inside the class but outside the scope of any
method are called instance variables in Java.
o The instance variable is initialized at the time of the class loading or when an
object of the class is created.
o An instance variable can be declared using different access modifiers available
in Java like default, private, public, and protected.
o Instance variables of different types have default values that are specified in the
next point.
Features
1. To use an instance variable an object of the class must be created.
2. An instance variable is destroyed when the object it is associated with is
destroyed.
3. An instance variable does not compulsory need to be initialized.
4. Instance variables are accessible inside the same class that declares them.

Limitations of Instance Variable


1. It cannot be declared static, abstract, striftp, synchronized, and native.
2. It can be declared final and transient.
3. It can be of any of the four access modifiers available in Java (private, public,
protected, and default).

Default Values of Instance Variables in Java

The instance variables in Java are of different data types as follows;

Instance variable type Default values

Boolean false

Byte (byte) 0

Short (short) 0

Int 0

Double 0.0d

Float 0.0

Long 0L
Object null

Char \u0000

Instance Methods in Java


 Instance Methods are the group of codes that performs a particular task.
Sometimes the program grows in size, and we want to separate the logic of the
main method from other methods. A method is a function written inside the
class. Since java is an object-oriented programming language, we need to write
a method inside some classes.

The important points regarding instance variables are:

1. Instance methods can access instance variables and instance methods


directly and undeviating.
2. Instance methods can access static variables and static methods directly.

Instance Method without parameter


Syntax:
modifier return_type method_name( )

method body ;

 modifier: It defines the access type of the method, and it is optional to use.
 return_type: Method may return a value. Ex:- int, void, String, char, float,
etc.
 method_name: This is the method name you can write anything as you
write the variable name.
 method body: The method body describes what the method does with
statements.

Algorithm :

• Create a class
• Set instance variables and methods
• Access them using dot operator

Code :

class Dog { class DogTestDrive {


int size; public static void main (String[] args)
String name; {
void bark() { Dog one = new Dog();
if (size > 60) { one.size = 70;
System.out.println(Wooof! Wooof); Dog two = new Dog();
} else if (size > 14) { two.size = 8;
System.out.println(Ruff! Ruff!); Dog three = new Dog();
} else { three.size = 35;
System.out.println(Yip! Yip!); one.bark();
} two.bark();
} three.bark();
} }
}

Output :

Conclusion :

We have Successfully implemented the code for the given aim.

Observations and learning:

Successfully Illustrated a class with member and methods by accessing


the instance variables and instance methods using dot (.) operator and displayed it.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy