SlideShare a Scribd company logo
Computer
Programming 2
Lesson 4– ASSESSING INSTANCE
VARIABLES
Prepared by: Analyn G. Regaton
ASSESSING
INSTANCE
VARIABLES
AND
METHODS
Instance variables and methods are accessed
via created objects. To access an instance
variable, following is the fully qualified path −
/* First create an object */
ObjectReference = new Constructor();
/* Now call a variable as follows */
ObjectReference.variableName;
/* Now you can call a class method as follows */
ObjectReference.MethodName();
This example
explains how to
access instance
variables
and methods of
a class.
public class Puppy {
int puppyAge;
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Name chosen is :" + name );
}
public void setAge( int age ) {
puppyAge = age;
}
public int getAge( ) {
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args) {
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
If we compile and run the above
program, then it will produce the
following result −
Output
Name chosen is :tommy
Puppy's age is :2
Variable Value :2
SOURCE FILES
DECLARATION
RULES
These rules are essential
when declaring
classes, import statements
and package statements in
a source file.
 There can be only one public class per source file.
 A source file can have multiple non-public classes.
 The public class name should be the name of the source file as
well which should be appended by .java at the end. For example:
the class name is public class Employee{} then the source file
should be as Employee.java.
 If the class is defined inside a package, then the package
statement should be the first statement in the source file.
 If import statements are present, then they must be written
between the package statement and the class declaration. If
there are no package statements, then the import statement
should be the first line in the source file.
 Import and package statements will imply to all the classes
present in the source file. It is not possible to declare different
import and/or package statements to different classes in the
source file.
IMPORT
STATEMENT
Import statement is a way of giving
the proper location for the compiler
to find that particular class.
For example, the following line would ask the
compiler to load all the classes available in
directory java_installation/java/io −
import java.io.*;
ASIMPLE
CASESTUDY
 For our case study, we will be creating two classes. They
are Employee and EmployeeTest.
 First open java editor(eclipse) and add the following
code. Remember this is the Employee class and the class
is a public class. Now, save this source file with the name
Employee.java.
 The Employee class has four instance variables - name,
age, designation and salary. The class has one explicitly
defined constructor, which takes a parameter.
EXAMPLE 1
import java.io.*;
public class Employee {
String name;
int age;
String designation;
double salary;
// This is the constructor of the class Employee
public Employee(String name) {
this.name = name;
}
// Assign the age of the Employee to the variable
age.
public void empAge(int empAge) {
age = empAge;
}
/* Assign the designation to the variable
designation.*/
public void empDesignation(String empDesig)
{
designation = empDesig;
}
/* Assign the salary to the variable salary.*/
public void empSalary(double empSalary) {
salary = empSalary;
}
/* Print the Employee details */
public void printEmployee() {
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" +
designation );
System.out.println("Salary:" + salary);
}
}
This will not run because it should be in a main method.
Note:Try this under
Employee java project.
Just follow the
instruction from previous
mentioned.
EXAMPLE 2
USING MAIN
METHOD
 As mentioned previously in this tutorial,
processing starts from the main method.
Therefore, in order for us to run this Employee
class there should be a main method and objects
should be created. We will be creating a
separate class for these tasks.
 Following is the EmployeeTest class, which
creates two instances of the class Employee and
invokes the methods for each object to assign
values for each variable.
 Save the following code in EmployeeTest.java
file.
EmployeeTest
code
import java.io.*;
public class EmployeeTest {
public static void main(String args[]) {
/* Create two objects using constructor */
Employee empOne = new Employee("James Smith");
Employee empTwo = new Employee("Mary Anne");
// Invoking methods for each object created
empOne.empAge(26);
empOne.empDesignation("Senior Software Engineer");
empOne.empSalary(1000);
empOne.printEmployee();
empTwo.empAge(21);
empTwo.empDesignation("Software Engineer");
empTwo.empSalary(500);
empTwo.printEmployee();
}
}
Now, compile both the classes and
then run
EmployeeTest to see the result as
follows −
Output
Name:James Smith
Age:26
Designation:Senior Software Engineer
Salary:1000.0Name:Mary Anne
Age:21
Designation:Software Engineer
Salary:500.0
Note:Try this under
EmployeeTest java
project. Just follow the
instruction from previous
mentioned.

More Related Content

PPTX
Polymorphism in C# Function overloading in C#
PPTX
encapsulation, inheritance, overriding, overloading
PPTX
Ppt on this and super keyword
PPT
Chapter 8 Inheritance
PPTX
Lecture 8 abstract class and interface
PPTX
Lecture - 5 Control Statement
PPT
Introduction to OOP with PHP
Polymorphism in C# Function overloading in C#
encapsulation, inheritance, overriding, overloading
Ppt on this and super keyword
Chapter 8 Inheritance
Lecture 8 abstract class and interface
Lecture - 5 Control Statement
Introduction to OOP with PHP

What's hot (20)

PPTX
Lecture - 3 Variables-data type_operators_oops concept
PPTX
Lecture 6 inheritance
PPTX
Lecture 4_Java Method-constructor_imp_keywords
PPTX
Functions parameters
PPTX
Inheritance in java
PDF
Method overloading, recursion, passing and returning objects from method, new...
PPTX
Only oop
PPTX
Functions1
PPTX
Subroutines in perl
PPTX
Lecture 9 access modifiers and packages
PPT
Oops concepts in php
PPTX
Functions
PPT
Super keyword.23
PPTX
Understanding Subroutines and Functions in VB6
PPT
Java adapter
PDF
Built in classes in java
PDF
Object Oriented Programming - 7.2. Polymorphism
PPTX
OOPS Basics With Example
PPTX
Classes, objects in JAVA
PPT
Chapter 2 - Getting Started with Java
Lecture - 3 Variables-data type_operators_oops concept
Lecture 6 inheritance
Lecture 4_Java Method-constructor_imp_keywords
Functions parameters
Inheritance in java
Method overloading, recursion, passing and returning objects from method, new...
Only oop
Functions1
Subroutines in perl
Lecture 9 access modifiers and packages
Oops concepts in php
Functions
Super keyword.23
Understanding Subroutines and Functions in VB6
Java adapter
Built in classes in java
Object Oriented Programming - 7.2. Polymorphism
OOPS Basics With Example
Classes, objects in JAVA
Chapter 2 - Getting Started with Java
Ad

Similar to Computer programming 2 -lesson 4 (20)

PDF
Lect 1-java object-classes
PPTX
PPTX
Simple class and object examples in java
PPTX
classes-objects in oops java-201023154255.pptx
PPTX
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
PPT
3. Data types and Variables
PPT
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
PPTX
introduction_OOP for the java courses [Autosaved].pptx
PPSX
Esoft Metro Campus - Certificate in java basics
PPT
packages and interfaces
PPT
Explain Classes and methods in java (ch04).ppt
PPTX
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
PPTX
Week9 Intro to classes and objects in Java
PPTX
Unit3 part1-class
PPSX
Java session4
PPTX
03 Java Language And OOP Part III
DOCX
OOP Lab Report.docx
PPTX
Classes objects in java
PPTX
cs213Lecture_1 java programming oopsss.pptx
DOCX
Computer Programming 2
Lect 1-java object-classes
Simple class and object examples in java
classes-objects in oops java-201023154255.pptx
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
3. Data types and Variables
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
introduction_OOP for the java courses [Autosaved].pptx
Esoft Metro Campus - Certificate in java basics
packages and interfaces
Explain Classes and methods in java (ch04).ppt
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Week9 Intro to classes and objects in Java
Unit3 part1-class
Java session4
03 Java Language And OOP Part III
OOP Lab Report.docx
Classes objects in java
cs213Lecture_1 java programming oopsss.pptx
Computer Programming 2
Ad

More from MLG College of Learning, Inc (20)

PPTX
PPTX
PC111-lesson1.pptx
PPTX
PC LEESOON 6.pptx
PPTX
PC 106 PPT-09.pptx
PPTX
PPTX
PPTX
PPTX
PC 106 Slide no.02
PPTX
PPTX
PPTX
PC 106 Slide 1.pptx
PDF
Db2 characteristics of db ms
PDF

Recently uploaded (20)

PPTX
master seminar digital applications in india
PDF
Computing-Curriculum for Schools in Ghana
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
RMMM.pdf make it easy to upload and study
PDF
01-Introduction-to-Information-Management.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Lesson notes of climatology university.
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
Computing-Curriculum for Schools in Ghana
Supply Chain Operations Speaking Notes -ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O7-L3 Supply Chain Operations - ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
RMMM.pdf make it easy to upload and study
01-Introduction-to-Information-Management.pdf
Sports Quiz easy sports quiz sports quiz
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Lesson notes of climatology university.
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Insiders guide to clinical Medicine.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Anesthesia in Laparoscopic Surgery in India

Computer programming 2 -lesson 4

  • 1. Computer Programming 2 Lesson 4– ASSESSING INSTANCE VARIABLES Prepared by: Analyn G. Regaton
  • 2. ASSESSING INSTANCE VARIABLES AND METHODS Instance variables and methods are accessed via created objects. To access an instance variable, following is the fully qualified path − /* First create an object */ ObjectReference = new Constructor(); /* Now call a variable as follows */ ObjectReference.variableName; /* Now you can call a class method as follows */ ObjectReference.MethodName();
  • 3. This example explains how to access instance variables and methods of a class. public class Puppy { int puppyAge; public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Name chosen is :" + name ); } public void setAge( int age ) { puppyAge = age; } public int getAge( ) { System.out.println("Puppy's age is :" + puppyAge ); return puppyAge; } public static void main(String []args) { /* Object creation */ Puppy myPuppy = new Puppy( "tommy" ); /* Call class method to set puppy's age */ myPuppy.setAge( 2 ); /* Call another class method to get puppy's age */ myPuppy.getAge( ); /* You can access instance variable as follows as well */ System.out.println("Variable Value :" + myPuppy.puppyAge ); } } If we compile and run the above program, then it will produce the following result − Output Name chosen is :tommy Puppy's age is :2 Variable Value :2
  • 4. SOURCE FILES DECLARATION RULES These rules are essential when declaring classes, import statements and package statements in a source file.  There can be only one public class per source file.  A source file can have multiple non-public classes.  The public class name should be the name of the source file as well which should be appended by .java at the end. For example: the class name is public class Employee{} then the source file should be as Employee.java.  If the class is defined inside a package, then the package statement should be the first statement in the source file.  If import statements are present, then they must be written between the package statement and the class declaration. If there are no package statements, then the import statement should be the first line in the source file.  Import and package statements will imply to all the classes present in the source file. It is not possible to declare different import and/or package statements to different classes in the source file.
  • 5. IMPORT STATEMENT Import statement is a way of giving the proper location for the compiler to find that particular class. For example, the following line would ask the compiler to load all the classes available in directory java_installation/java/io − import java.io.*;
  • 6. ASIMPLE CASESTUDY  For our case study, we will be creating two classes. They are Employee and EmployeeTest.  First open java editor(eclipse) and add the following code. Remember this is the Employee class and the class is a public class. Now, save this source file with the name Employee.java.  The Employee class has four instance variables - name, age, designation and salary. The class has one explicitly defined constructor, which takes a parameter.
  • 7. EXAMPLE 1 import java.io.*; public class Employee { String name; int age; String designation; double salary; // This is the constructor of the class Employee public Employee(String name) { this.name = name; } // Assign the age of the Employee to the variable age. public void empAge(int empAge) { age = empAge; } /* Assign the designation to the variable designation.*/ public void empDesignation(String empDesig) { designation = empDesig; } /* Assign the salary to the variable salary.*/ public void empSalary(double empSalary) { salary = empSalary; } /* Print the Employee details */ public void printEmployee() { System.out.println("Name:"+ name ); System.out.println("Age:" + age ); System.out.println("Designation:" + designation ); System.out.println("Salary:" + salary); } } This will not run because it should be in a main method. Note:Try this under Employee java project. Just follow the instruction from previous mentioned.
  • 8. EXAMPLE 2 USING MAIN METHOD  As mentioned previously in this tutorial, processing starts from the main method. Therefore, in order for us to run this Employee class there should be a main method and objects should be created. We will be creating a separate class for these tasks.  Following is the EmployeeTest class, which creates two instances of the class Employee and invokes the methods for each object to assign values for each variable.  Save the following code in EmployeeTest.java file.
  • 9. EmployeeTest code import java.io.*; public class EmployeeTest { public static void main(String args[]) { /* Create two objects using constructor */ Employee empOne = new Employee("James Smith"); Employee empTwo = new Employee("Mary Anne"); // Invoking methods for each object created empOne.empAge(26); empOne.empDesignation("Senior Software Engineer"); empOne.empSalary(1000); empOne.printEmployee(); empTwo.empAge(21); empTwo.empDesignation("Software Engineer"); empTwo.empSalary(500); empTwo.printEmployee(); } } Now, compile both the classes and then run EmployeeTest to see the result as follows − Output Name:James Smith Age:26 Designation:Senior Software Engineer Salary:1000.0Name:Mary Anne Age:21 Designation:Software Engineer Salary:500.0 Note:Try this under EmployeeTest java project. Just follow the instruction from previous mentioned.