- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between Java classes and Java objects?
A class can be defined as a template/blueprint that describes the behaviour/state that the object of its type support.
Example
public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } }
Objects have states and behaviours. Example: A dog has states - colour, name, breed as well as behaviours – wagging the tail, barking, eating. An object is an instance of a class.
An object is created from a class. In Java, the new keyword is used to create new objects.
There are three steps when creating an object from a class −
Declaration − A variable declaration with a variable name with an object type.
Instantiation − The 'new' keyword is used to create the object.
Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
Following is an example of creating an object −
Example
public class Puppy { public Puppy(String name) { // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public static void main(String []args) { // Following statement would create an object myPuppy Puppy myPuppy = new Puppy( "tommy" ); } }
Output
Passed Name is :tommy
- Related Articles
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are Java classes?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between recursion and iteration in Java?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JTextField and JTextArea in Java?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?
- What are Java parent and child classes in Java?
- What are the differences between the TableCellRenderer and TableCellEditor in Java?
- What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?\n
- What are the differences between length and length () in Java?\n
- What are the differences between JFrame and JDialog in Java?\n
- What are the differences between JavaScript Primitive Data Types and Objects?
- What is the difference between static classes and non-static inner classes in Java?

Advertisements