A Basic introduction to Bean class, reference variables …

Today was the first day of ‘Cloud computing with android’ training. We were introduced to the basic concepts of POJO/ Bean/ Model, reference variable, single value container, multivalue container, where the objects are stored, garbage collector.

For making any software, the prime thing is to know what are the requirements. They are the objects which consists of all the requirements i.e. attributes. For example, we have Product object. It can consists of attributes such as name, price, weight of product.

Bean/ POJO/ Model: A class with attributes, constructors, getters and setters.

Example of Bean class:


package com.mitaly.bean;

public class Product {
//attributes
String name;
int price;
float weight;

//constructor
public Product(String name, int price, float weight){
this.name = name;
this.price = price;
this.weight = weight;
}

//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}

Single value and Multi-value container: Those variables are called as single value container which consists of single value and those variables which can contain more than one value are known as multi-value container. For example,

int = 20; //single value container

int[] arr = {10,20,30,40} //multi-value container

Product p1 = new Product (‘Dairy milk’, 20,  10); //Multi-value container

Storage:

Storage of objects, variables and reference variables

In i = 10, ‘i’ is a normal variable. It can store ‘literals’. Literals are stored in a place known as ”Permanent Generation’. Suppose, we have another variable, int j = 20; in this case ‘j’ will contain value 20(which comes from Permanent Generation). There will be only single copy of ’20’ in Permanent Generation area irrespective of number of variables i,j,… which has value 20.

For multi-value containers like array and reference variables, the stack is going to contain the address of objects which are created in Heap area. So, objects don’t have any name. They are just created in Heap area. Just there starting  address is copied into respective reference variable in Stack.  Like, we have array ‘arr’ storing 5001 which is the address for array object created in heap.

We have studied above that ‘p1’ is a reference variable. The object to which it points consists of attributes and the methods.

Garbage Collector: In Java, garbage collector automatically deletes the unnecessary objects. We have two types of space:

  • Old space
  • Young space

Mark and sweep algorithm is used which marks the useless objects and move them to ‘Old Space’. ‘Young Space’ consists of new objects and variables.

Leave a comment