What is an Object in Java?

Objects in software is a bundle that contains state and behavior .We use objects in our daily lives Example of objects are- Dog Des...

Objects in software is a bundle that contains state and behavior.We use objects in our daily lives Example of objects are-

  • Dog
  • Desk
  • Car etc.

All objects have to characteristics:


  • State –Eg Cow- has(name,color,breed etc)
  • Behaviour-Eg Car has behavior like (changing gear, four wheel drive, apply breaks etc.)

Importance of bundling software objects


  • Modularity-it’s easy to write and maintain object code.These objects can be passed around the system easily.
  • Information hiding-Because users interact with objects, it's easy to hide internal implementation of a software.
  • Enables code reuse-it’s easy to reuse objects once created.
  • Easy to debug-if a certain object does not work properly, its easy to remove that object.



How to create a Class Object

Creating a class object is just easy. You just need to use new keyword then call the class constructor.

Example.


/**
 *
 * @author Eric
 * www.techoverload.net
 * How to create class object
 */

public class Employees {
    //class instance variables
    public int id;
    public String name;
    public String address;
    public int age;
    //class setter and getter methods
    public void setAge(int employeeAge)
    {
    age=employeeAge;
    }
    public void setName(String employeeName)
    {
    name=employeeName;
    }
    public void setAddress(String employeeAddress)
    {
    address=employeeAddress;
    }
    public void setId(int employeeId)
    {
    id=employeeId;
    }
    public int getId()
    {
    id=this.id;
    return id;
    }
    
     public int getAge()
    {
    age=this.age;
    return age;
    }
     public String getName()
    {
    name=this.name;
    return name;
    }
     
     public String getAddress()
    {
    address=this.address;
    return address;
    }
     //Static main method
     public static void main(String args[])
     {
         //creating class object using class constructor
     Employees em=new Employees();
     em.setAge(23);
     em.setAddress("PO BOX 11 Mombasa");
     em.setId(234532345);
     em.setName("Mellisa");
     System.out.println("ID " +em.id);
     System.out.println("Age " +em.age);
     System.out.println("Name " +em.name);
     
     System.out.println("Address " +em.address);
     
     }
   
}

Click here to Download Above Example Code

COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: What is an Object in Java?
What is an Object in Java?
Code WHIZZ
https://code-whizz.blogspot.com/2017/10/what-is-object-in-java.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/10/what-is-object-in-java.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy