Common types of java objects
Java Objects
An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :
- State: It is represented by attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.

Declaring Objects
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.
As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variables , the type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface.
Initializing a Java Object
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.
// Class Declaration
public class Dog {
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class
public Dog(String name, String breed, int age,
String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
// method 1
public String getName() { return name; }
// method 2
public String getBreed() { return breed; }
// method 3
public int getAge() { return age; }
// method 4
public String getColor() { return color; }
@Override public String toString()
{
return ("Hi my name is " + this.getName()
+ ".\nMy breed,age and color are "
+ this.getBreed() + "," + this.getAge()
+ "," + this.getColor());
}
public static void main(String[] args)
{
Dog tuffy
= new Dog("tuffy", "papillon", 5, "white");
System.out.println(tuffy.toString());
}
}
String[] words = {"main"};
Dog.main(words);
Hi my name is tuffy.
My breed,age and color are papillon,5,white
Initialize by using method/function:
public class GFG {
// sw=software
static String sw_name;
static float sw_price;
static void set(String n, float p)
{
sw_name = n;
sw_price = p;
}
static void get()
{
System.out.println("Software name is: " + sw_name);
System.out.println("Software price is: "
+ sw_price);
}
public static void main(String args[])
{
GFG.set("Visual studio", 0.0f);
GFG.get();
}
}
String[] words = {"main"};
GFG.main(words);
Software name is: Visual studio
Software price is: 0.0