Tutorial Java – #6 Classes and objects

Classes represent the basic principles of object-oriented programming. Classes are abstract concepts, stories, blueprints, describing:

  • characteristics, attributes of an object; these represent what the object knows, what it is;
  • object methods; these represent what the object knows to do, its behavior;

Based on classes, the programmer can define something concrete, objects. Thus, by instantiating the class (attributes defined in class are initialized with values) are obtained object type variables. For those who have C programming knowledge, a class can be considered a structure defined by struct, in which you can define also methods.

Other topics that are part of this Java tutorial are accessible through Java 6 Tutorial – Contents.

Object Oriented Programming describes a methodology by which software application can be decomposed into objects that represent components developed to solve the smaller problems. Object Oriented Programming objective is to break down complex solutions into smaller problems (classes) which are more easily managed. Other object-oriented programming concepts (which will be detailed in separate posts):

  • encapsulation – hides as much of the internal implementation of the class; object attributes are not accessible directly but only through the interface (collection of functions); regarding methods, outside the class it is known only what the class does, but the user of the class does not know how it is implemented and he can not change the solution;
  • derivation/inheritance – allows the further development of existing classes and building new solutions based on existing one without altering them; in C there is a real problem for managing complex solutions because we have 2 separate entities, functions and structures, and every time you need to change something these will affect major parts of the existing solution;
  • polymorphism – allows implementation of different solutions (methods) under the same name.

How to define a class in Java

The syntax used in Java 6 to define a class is:

[access_modifier] class class_name [extends base_class] [implements interface1, interface2, …]
//class code block start 
//initialization code blocks
//other classes

}//end of class code block


access modifier – describes the access rights for the class from a Java program; this attribute is optional, with a default value equal to private; although there is another post dedicated just to understand the access modifiers, now we keep in mind the possible values:

  • public – in terms of security, the class can be used anywhere in Java;
  • protected
  • private

class – the keyword that defines this structure in Java;

class name – class name defined by the programmer; class names must follow the same naming conventions as well as any variable (see the post about Java variables and naming conventions)

extends – allows derivation from a base class (more in the tutorial about the derivation/inheritance)

implements – allows the derivation from one or more interfaces (more in the tutorial about interfaces)

The simplest syntax required to define a class is:

class nume_clasa 
//class code block start
//initialization code blocks
//other classes
}//end of class code block

Regarding attributes we can define in a class:

  • instance variables or attributes of objects;
  • static variable – a type of “global variables”.

Regarding methods (functions) we can define in a class:

  • constructor methods;
  • access functions(get and set);
  • processing methods;

Regarding the declarations of attributes and methods there are no rules on how they should be defined, but code management is easier if the code and attributes are defined grouped.

There are some rules regarding how to define a class:
  1. in a Java file, .java , can be defined several classes;
  2. in a Java file, can be defined only one public class;
  3. the Java source file containing the public class has the same name as the public class (case sensitive); Book.java contains public class Book ;
  4. the code block of a class is defined by { and };
  5. if in a source file, .java, are defined more classes, then by compiling the source file there are obtained bytecode files, .class, for each class.

Based on the simple syntax, we define a simple Java class that describes a book. The book is described by its attributes price, title and author , and the behavior is defined by its methods getPret() and display().

class Book{
    //define attributes - instance variables
    float price;
    String title;
    String author;

    //define methods
    public float getPrice(){
        return price;
    public String display(){
        return "The book "+titlu+" has as author "+author;

How to construct objects

If the class represents something abstract, then objects represent something real. Objects are instances of the class because the story behind the construction of an object class becomes something real: a space memory in Heap, in which the instance attributes have values.

Objects are constructed by the new  operator that will call the class constructor (with or without parameters).

To test the class Book defined earlier, we use a new public class, TestBook (Java file is also called TestBook.java):

public class TestBook {
    public static void main(String[] args) {
	//define references
	Book book1;	//are valoarea null
	Book book2 = null;
	//create objects
        book1 = new Book();
	book2 = new Book();

The object variable that represents a reference (pointer) manages an address from the Heap.Through this address we have access to the memory area reserved for the object. In this zone we find its attributes values.

By defining a simple class variable, you get a reference with the default value equal to null. To give a value to this reference you must built (instantiates) the object using new.

How to access object methods and attributes

The object has access to its attributes and methods (not static ones) through the . (point) operator.

public class TestBook {
    public static void main(String[] args) {
        Book book1 = new Carte();
	book1.price = 23;
        book1.title = "Dune";
        book1.author = "Frank Herbert";
        Book book2 = new Carte();
	book2.price = 35;
        book2.title = "Harry Potter";
        book2.author = "J.K. Rowling";

Access to object attributes and methods outside the class, as in the previous example in which the object is used in TestBook class, can be controlled by access modifiers (public, private, protected, that will be analyzed in detail in another post).

How objects look in memory

To understand the principles of OOP in Java it is important to note that in Java, all objects (eg Book type variables) are references and the objects values (given by its attribute values) are in the Heap memory area.

The two objects of type Book, constructed in the above example, look in memory (notice this is a simplified representation because String is also an object, which means it is also a reference variable and its value it is in the Heap) like this:

Objects in Memory
Objects in Memory

Other topics that are part of this Java 6 tutorial are accessible through Java 6 Tutorial – Contents.