Tampilkan postingan dengan label class. Tampilkan semua postingan
Tampilkan postingan dengan label class. Tampilkan semua postingan

What is a Class

Selasa, 18 Maret 2014

Welcome back to Java Code Online. Class is the most commonly thing in Java. Having a good understanding of class is essential for getting a firm grip on the Principle of OOPS and Java itself. A class is called the blueprint of an Object. Everything that is done in Java, has to be enclosed within a class.

As I said before that the class is a blueprint of an Object, it is used for making Objects. I Java we know that everything is an Object, and if we dont know then we must know this. Java is built on the Principle of OOPS and is totally Object Oriented. So Java see everything as an Object.

I will take you through a through understanding of this concept. A class is used to define a new data type, and normally a separate class is used to make Objects of this new data type. Think of a class as something which defines what an Object knows and what an Object does. Therefore a class is also called a template for an Object and an Object is called an instance of the class. Thus a class contains the instance variables (things which an Object knows) and methods (things which an Object does).

A class is just like a blueprint and it covers every intricate detail about the Object that will be made from it. The normal structure of class is that it defines everything about an Object, so suppose that your application consists of some Objects, and so you make classes for each. Now you define a main class which will initiate all the calling procedure.

The main class always contain the method "main". This method tell the JVM that this is the main class and it need to start execution from here. Once in the main class, you can define Objects of other classes that you have made. I will take a simple example to clarify my point.

// This program declares two Car objects.
class Square {
int width;
int height;
}

class SquareDemo2 {
public static void main(String args[]) {
Square mySquare1 = new Square();
Square mySquare2 = new Square();
int area;
// assign values to mySquare1s instance variables
mySquare1.width = 10;
mySquare1.height = 20;
/* assign different values to mySquare2s instance variables */
mySquare2.width = 15;
mySquare2.height = 15;
// compute area of first Square
area = mySquare1.width * mySquare1.height;
System.out.println("Area is " + area);
// compute area of second box
area = mySquare2.width * mySquare2.height;
System.out.println("Area is " + area);
}
}

The first System.out.println results in:- 200
The second System.out.println results in:- 225


Now you must be wondering about so many new things, I have shown above. I will cover each of them.

Here we have defined two classes Square and SquareDemo2. The first class is used for making an Object of type Car. The second Class uses this new data type for making new Objects. Now as told before, the execution begins from the class containing the "main" method.

Now as you can see that there are two different outputs for the two Objects, this shows that both the Objects are unique and are independent of each other. Here System.out.println is used for displaying the results at the console or output screen.

The lines starting with // are skipped by the Java compiler. Infact the // is the syntax for Comment in Java. These are very helpful in explaining the functioning of the code.

If you have any problems with the code that I have explained right now, then do leave me a comment and I will try my best to clarify your doubt.

The most important thing that you must have noticed here is the parameter "new". This is the magic keyword of Java and is used for creating new Objects. I will discuss more on creating a new Object in my later posts.

I hope the article was helpful in explaining you about What is a Class. If you like this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Read More..

Difference between Singleton Pattern vs Static Class in Java

Kamis, 13 Februari 2014

Singleton pattern  vs  Static Class (a class, having all static methods) is another interesting questions, which I missed while blogging about Interview questions on Singleton pattern in Java. Since both Singleton pattern and static class provides good accessibility, and they share some similarities e.g. both can be used without creating object and both provide only one instance, at very high level it looks that they both are intended for same task. Because of high level similarities, interviewer normally ask questions like, Why you use Singleton instead of Static Methods, or Can you replace Singleton with static class, and  what are differences between Singleton pattern and static in Java. In order to answer these question, it’s important to remember fundamental difference between Singleton pattern and static class, former gives you an Object, while later just provide static methods. Since an object is always much more capable than a method, it can guide you when to use Singleton pattern vs static methods.
Read more »
Read More..

Why Default or No Argument Constructor is Important in Java Class

Kamis, 06 Februari 2014

Almost all Java developers knows that compiler adds a default constructor or better known as no argument constructor in every Java class, but many of them forget that, it only does when you don't provide any other constructor. Which means it becomes developers responsibility to add a no argument constructor, if he is adding explicit constructor. Now, Why it's important to provide default constructor in Java, What happens if your class don't have a no argument constructor? Well, this is how it's asked in many Java interviews, most commonly as part of Spring and Hibernate interviews. It's not mandatory to define default constructor, but if you are writing Hibernate persistent class, JPA entities or using Spring framework to manage object creation and wiring dependencies, you need to be bit careful. Many of open source framework, uses reflection to create instance or Object at runtime, based upon name of class. For example When Hibernate creates instance of entities using reflection it uses Class.newInstance() method, which require a no argument constructor to create an instance. It's effectively equivalent of new Entity(). This method throws InstantiationException if it doesn't found any no argument constructor in Entity class, and that's why it's advised to provide a no argument constructor.
Read more »
Read More..