Introduction to OOP: POJO
This chapter will teach us about scope, setter method, getter method, and, POJO
Introduction to OOP: POJO
This chapter will teach us about scope, setter method, getter method, and, POJO

**Previous Chapter [First Chapter](https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66)**
Introduction to scope.
As usual, we will start with our Toy class example.
public class Toy {
private int ID;
private int price;
private int quantity;
public void setID(int id) {
ID = id;
}
public void setPrice(int p) {
price = price;
}
public void setQuantity(int q) {
quantity = q;
}
}
We will discuss the scope in terms of variables. A scope defines the boundary of a variable where it can be accessed.
Block scope: Any variable declared inside a block is only accessible within the block. Remember though it is not possible to access the variable before declaration within the block. This is called block scope.
[embed]
A block is defined by the two curly braces (opening and closing). In the above program, a block is started at line 3 and ended in line 7.
If you compile the above program, the compiler will show you an error saying that it does not find the id at line no 9.
Method scope: A method scope is similar to a block scope. A method scope starts at the opening curly braces after the method signature and ends at the closing curly braces. The rules for accessing variables are the same as the block scope. The parameters are also considered variables inside the method scope.
Class scope: A class scope starts at the opening curly braces after the class name and ends at the closing curly braces.
public class Toy {
private int ID;
private int price;
private int quantity;
public void setID(int id) {
ID = id;
}
public void setPrice(int p) {
price = price;
}
public void setQuantity(int q) {
quantity = q;
}
}
The variables ID, price, and quantity are variables inside the class. It can be accessed inside any method or any block. The class scope can also be called global scope.
The lifetime of the local variable: A local variable is a variable declared inside a block or method. The memory for a local variable is allocated by the JVM when the variable declaration statement is executed inside a block or a method. The memory of the local variable is reclaimed by the JVM when the variable goes out of the block or a method.
The lifetime of the member variable: The lifetime of the member variable is tightly bound to the lifetime of the instance created from the class. We already know memory required for an instance is taken from the HEAP. The member variables are part of the instance created in the HEAP, so the memory allocated for the member variables is taken from the HEAP. When an instance is no longer required the memory of the instance is reclaimed by the JVM and when that happens the memory for the member variables is reclaimed also.
Variable Hiding
Variable declared in class scope are hidden by variable declared inside method scope. Let's see an example of this:
public class VariableHiding {
public int value = 10;
public void printValue() {
int value = 100;
System.out.println(value);
}
}
Here we have a local variable named value of type int inside the method which has the same name as the member variable named value of type int.
public class Main {
public static void main(String[] args) {
VariableHiding variableHiding = new VariableHiding();
variableHiding.printValue();
}
}
Now, if we run the program, we will see the following output:

Why is the output showing output? The reason is:
Here the local variable named value of type int hides the member variable named value which is also of type int. When the method printValue() is executed JVM sees the declaration of the local variable named value which contains 100 and prints the output.
Now variable hiding only takes into account the variable name and ignore types. Here is the modified version of the VariableHiding class:
public class VariableHiding {
public int value = 10;
public void printValue() {
char value = 'a';
System.out.println(value);
}
}
The Main.java remains the same. Now if you run the program, we will get:

You can see that the output shows the character ‘a’.
Now let's go back to the same data type example:
public class VariableHiding {
public int value = 10;
public void printValue() {
int value = 100;
System.out.println(value);
}
}
Now we want to print 10 instead of local variables value. This can be done using the this keyword as this acts as a reference variable to the instance created. Here is the code:
public class VariableHiding {
public int value = 10;
public void printValue() {
int value = 100;
System.out.println(this.value);
}
}
The output of compiling and running Main.java:

Now you can see that 10 is printed. So, whenever variable hiding occurs in your code and you want to access an instance's member variable, use this like above.

Let’s modify our Toy class to use this:
public class Toy {
private int ID;
private int price;
private int quantity;
public void setID(int ID) {
this.ID = ID;
}
public void setPrice(int price) {
this.price = price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
If we take setID as an example its parameter name ID is the same as the member variable named ID. We want to assign the value passed to the parameter ID to the member variable named ID we needed to use this. If we do not use this then we will end up assigning local variable ID to itself.
PascalCase Naming Convention
We are using this naming convention in naming the classes so in Java. In this convention, the first character of every word in the name of a class must be in uppercase and other characters must be in lowercase. We have already used this convention. For example, the class name VariableHiding. The name VariableHiding contains two words variable and hiding. The word variable is made Variable and hiding is made Hiding. The first characters of both of those words are made bold.
CamelCase Naming Convention
We are using this convention in naming the variables and methods in Java. This convention is similar to the pascalcase convention except that the first character of the first word must be in lowercase. For example, grossProfit, setID, setPrice, setQuantity, etc.
Setter Methods
public class Toy {
private int ID;
private int price;
private int quantity;
public void setID(int ID) {
this.ID = ID;
}
public void setPrice(int price) {
this.price = price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
In the above code setID, setPrice, and setQuantity methods are called setter methods. They are called setter methods because they are used to set the value of the instance or member variables of an instance. The naming convention for those methods:
- use the word set
- after that write the member variable name in pascalcase naming convention.
For example, if we have a member variable named sellingPrice then setter method name must be setSellingPrice. The SellingPrice follows pascalcase naming convention.
Getter Methods
Now let's see our Toy class definition once again:
public class Toy {
private int ID;
private int price;
private int quantity;
public void setID(int ID) {
this.ID = ID;
}
public void setPrice(int price) {
this.price = price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
The Main.java:
public class Main {
public static void main(String[] args) {
Toy car = new Toy();
car.setID(10);
car.setPrice(30);
car.setQuantity(10);
}
}
In the Main.java we are only setting values for the member or instance variables. But, now we want to print those values for the instance car. How should we do it? we cannot directly access it like:
System.out.println(car.ID);
The answer is we again need a method to get the values. To do that we need to include getter methods.
Getter Methods
Here is the new implementation after including getter methods:
public class Toy {
private int ID;
private int price;
private int quantity;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
The methods getID, getPrice, getQuantity are getter methods. They return the value contained by the instance variables. Let’s analyze getID() getter method:
The return type of the method is not void, it is int as we want to return the value of ID and ID has type int.
public int getID() {
return ID;
}
The statement inside the body of the method is called the return statement. The return statement is used to return value from a method. Here, we return the value of the ID using the statement return ID;
Note: When you use the return statement the return type of the method must match the return type of the value. It is better to maintain this rule that's why I used must.
Here is the modified Main.java:
public class Main {
public static void main(String[] args) {
Toy car = new Toy();
car.setID(10);
car.setPrice(30);
car.setQuantity(10);
System.out.println("The id is: " + car.getID());
System.out.println("The price is: " + car.getPrice());
System.out.println("The quantity is: " + car.getQuantity());
}
}
Here is the output if you compile and run Main.java:

POJO (Plain Old Java Objects)
The Toy class we have written is a POJO. It only contains member variables, setter methods, and getter methods. There is no special logic attached to it. POJS are the classes that will only have member variables, setter method, and getter method. In member variables must be private, setter methods have only one statement that will set the value of the member variable, and getter methods must have only one statement which is a return statement.
This is it for this chapter.
메타데이터
- post_id
- d6ecc5fc17e4
- slug
- introduction-to-oop-pojo-d6ecc5fc17e4
- url
- https://medium.com/@nsupower/introduction-to-oop-pojo-d6ecc5fc17e4
- canonical_url
- https://medium.com/@nsupower/introduction-to-oop-pojo-d6ecc5fc17e4
- author_url
- https://medium.com/@nsupower
- status
- ok
- fetched_at
- 2026-07-28 01:54:09