What is Java Class and Object?
Table of Content
1. What is a Java Class?
- Create your first Java program "Hello World"
- Explanation of Java keywords used in "Hello World" program
2. What is a Java Object?
- How to create Java Object?
- What is the use of Java Objects?
3. What is Java Constructor?
- Non-Parameterized Constructor (Default)
- Parameterized Constructor
1. What is a Java Class?
- Fields (Variables)
- Methods
- Constructor
- Nested Class
- Interface
- Blocks
public class Dog { String breed; int age; String color; public void barking() { } public void hungry() { } }
- Variables like breed, age and color are attributes or exhibits state.
- Where as barking() and hungry() are methods which exhibits behavior.
Class:
Animal
Objects:
Dog
Cat
Cow
Now, you can relate the definition that Class is a template and object is an instance of the class.
Java Class and Object Video Tutorial
1.1. Write your First Java Program - Hello World
- Declare a class 'MyFirstProgram'
- Declare the main method public static void main()
- And print a string "Hello World" using System.out.println()
// Class
class MyFirstProgram {
// Main method
public static void main(String[] args) {
// Print Hello World
System.out.println("Hello World");
}
}
1.2. Explanation of Java keywords used in 'Hello World' Program:
- class keyword is used to declare a class.
- Public is an access modifier which defines main method's accessibility. Public means main method is global or accessible to all other classes in the project.
- static is a keyword in java, when static is used with any method then it is called static method. Static method can be called without creating the object of the class.
- main is the method name and also the execution point of any program. Main method is required to execute the program.
- String[] args is an array of string is basically for command line arguments.
- System.out.println() is used to print any statement.
- System is a class
- out is the object of PrintStream class
- println() is method of PrintStream class
- If you are using Eclipse then you can run this program by just clicking the green play button displayed on the top bar.
- Or right click anywhere in the class and click 'Run as Java Program'.
- Hello World
2. What is a Java Object?
We often read in our text books that "Object is an instance of a class". An object has a state and behavior. For example., a dog has states - color, name and behavior as well like barking, eating etc.
The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes. In Java, an object is created using the keyword "new".
2.1. How to create a Java Object?
MyClass myObject = new MyClass();
In above example,
- MyClass is class name.
- 'myObject ' is the object name (object name can be anything)
- 'new' is keyword of java which instantiates the object (in simple language it creates the object).
- 'MyClass()'is the constructor of 'MyClass' which is called whenever an object is created.
2.2. What are the uses of Java Objects?
- We can call methods of a class by using object of that class with dot (.) operator.
- Similarly, we can call variables of a class by using object of that class with dot (.) operator.
3. What is Java Constructor?
In java a constructor is called a special method. It is used to create objects in java. A constructor is called whenever we initialize an object.
Some special features of Java Constructor:
- It has same name as that of a class.
- It does not have any return type.
- It's syntax is similar to method.
- A java constructor cannot be static, final or abstract.
- A class always have a default constructor whether you have declared it or not. But if you define a custom constructor then default constructor is no longer in use.
Java Constructor Video Tutorial
3.1. Types of Constructors in Java:
- Non-Parameterized Constructor (Default Constructor)
- Parameterized Constructor
3.1.1. Non-Parameterized Constructor
public class MyClass{ // Creating a default constructor MyClass(){ System.out.println("Object created"); } // Main method public static void main(String args[]){ //Calling a default constructor or Creating an object of MyClass MyClass obj = new MyClass(); } }
Explanation of example code:
- We have created a default constructor "My Class", which will print "Object created" whenever we create an object.
- In main method, we are calling the constructor to initialize our object named "obj".
3.1.2. Parameterized Constructor
public class MyClass { // Declare variable int num; // Parameterized Constructor public MyClass(int y) { num = y; } public static void main(String[] args) { // Call the parameterized constructor and pass parameter 5 MyClass myObj = new MyClass(5); // Print num System.out.println(myObj.num); }
}
- We declared a variable num.
- Declared a parameterized constructor with parameter int y.
- Inside constructor we are initializing num's value to y.
- In main method, we are calling parameterized constructor and passing value 5 to y. It will initialize num to 5 inside the parameterized constructor.
- Now the value of the num is set to 5 and it'll print 5.
oh good! you bring good & unique content for us. I'm glad to read. Please keep sharing. - Mahindra Jivo 365
ReplyDeleteYou are giving such interesting information. It is great and beneficial info for us, I really enjoyed reading it. Thankful to you for sharing an article like this.dft training in bangalore
ReplyDeleteThanks for sharing beneficial tutorial. Web development, design and All SEO service provider Houston Digital Marketing Agency
ReplyDeleteThe blog you have shared is awesome about C Programming Assignment Help This is very useful for us. Thanks for sharing such a good blog.
ReplyDeleteGood to read this article when starting a blog. Happy to read that is only a limitation in our minds Thanks!
ReplyDeleteThis article was such an amazing masterpiece. I really want to know how did you get this information? I’m really thankful to you to provide this knowledge. Tablet hire is providing the best services for tablets, iPad, laptops, and many other technical devices at rent. we serve our services at a very responsible price.
ReplyDeletetab rentals