Java Class and Object Example

by Didin J. on Dec 09, 2016 Java Class and Object Example

Java class and object example in best practice using full source code with Netbeans 8.2 IDE

One of the essential features of Java programming is Object because java is pure object-oriented programming. Classes are blueprint or prototype of an object. A class in object-oriented programming is an extensible program-code-template for creating objects, providing initial values for state (member variables or fields) and implementations of behavior (member functions or methods). The class name is used as the name for the class (the template itself), the name for the default constructor of the class (a subroutine that creates objects), and as the type of objects generated by instantiating the class. These distinct concepts are easily conflated.

Table of Contents:

Here's I want to show you what is Java class and object example. The complete Java application, web application or Android application built by a set or more Java Classes and Objects.

Create a Java Application

Next, create a project using Netbeans 8.2 and Java 8 will be an easy way, name this new project with JavaClassExample and leave all properties to default. After the project generated, there will be a package name `javaclassexample` with the main Java class named JavaClassExample.

Create a New Java Class

Create new java class by right-clicking on the package the select New -> Java Class then name it Product.java. Next, type or put this code into this new java class.

package javaclassexample;

public class Product {
    
    String name = "LED Monitor";
    double price = 2000000;
    int quantity = 10;
    
    void changeName(String newName) {
        name = newName;
    }
    
    void changePrice(double newPrice) {
        price = newPrice;
    }
    
    void changeQuantity(int newQuantity) {
        quantity = newQuantity;
    }
    
    void printChanges() {
        System.out.println("Product: "+name+", Price: "+price+", Quantity: "+quantity);
    }
}

Here's we create an object Product with representing by Product class which has fields:

  1. String name
  2. double price
  3. int quantity

It also has methods for its fields, there are: 

  1. changeName
  2. changePrice
  3. changeQuantity 

All with their own parameter and one method to print fields changes to the console.

Calls Java Class in The Main Java Application

As you see there's a different data type in fields section which String is an object of text, double and it is primitive java types that represent numerically. And here's how to instantiate(call) an object and its method. Put this code into the body of the main java class.

package javaclassexample;

public class JavaClassExample {

    public static void main(String[] args) {
        
        // Create 2 product object
        Product product1 = new Product();
        Product product2 = new Product();
        
        // Invoke method on each objects
        product1.changeName("Keyboard");
        product1.changePrice(85000);
        product1.changeQuantity(100);
        product1.printChanges();
        
        product2.changeName("Mouse");
        product2.changePrice(55000);
        product2.changeQuantity(120);
        product2.printChanges();
    }
    
}

In the above code that shows 2 objects is created, there is product1 and product2. After the object created each of those objects call its methods and put/set a new parameter inside that methods. So, when we run this application, those object print new fields values by method printChanges(). And the results like this:

run:
Product: Keyboard, Price: 85000.0, Quantity: 100
Product: Mouse, Price: 55000.0, Quantity: 120
BUILD SUCCESSFUL (total time: 1 second)

That it's a simple class and object example for today.

That just the basic. If you need more deep learning about Java and Spring Framework you can take the following cheap course:

Thanks

Loading…