What is Java Class :-
To understand "What is a Java class and how to define it in Java ?",we are taking very simple java program.By this program, you can understand what is Class in java very easily? After that we will create a class definition so that you can answer this during your interview easily. I will be providing some rules to write a java class for a good programmer and some cases to check how to resolve the common java class errors while creating a java class in your project.Let's have a look at the below example:-
package java2selenium;
public class TestJava {
public static void main(String[] args) {
System.out.println("Welcome to Java2Selenium");
}
}
In the above example the TestJava is a user defined class.We can define a class by using one of the java keywords called "class". A java class always starts with '{' opening curly brace and ending with
closing brace'}'. By using this class name we can create a n- number of objects of same class type.We will discuss in next topic what is Object and how to create a object using a class.
To understand what is class more,let's assume one real life example in our daily life.Suppose, a builder in a city wants to make a n-number of buildings .Before developing the buildings, he need one map so that based on that he can build a building. The Map contains all rooms size and positions, stairs position and so on. The Map will work like a template and using this template we can create any number of buildings.
In the above example, map is a kind of class and n- number of buildings are Objects those are created based on this map. The rooms numbers,sizes and all will define the state and behaviour.
Definition:- "A java class is template or blueprint that describes the state and behavior of objects created using the same class."
A class can contain fields(variables) to describe the state and methods to describe the behavior of an object. Syntax:
--------------------------------------------------------------
<class modifier> <class keyword> class name
----------------------------------------------------------------------
Eg. public class TestJava or class TestJava
Class declaration:
class Map{
//fields declarations
//Methods declarations
//constructors declarations
}
The class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class.
Rules to declare a class:-
********************
1. A class name always starts with initial letter capitalized by convention.
2. The class body, surrounded by braces, {}.
3. Having modifiers such as public, private and default.
4. Attach with class keyword starts with small letter.
Cases:-
---------
Case 1: If you are making a class as source file name and not declaring that class name as public then you will get compile time error. A source file name must be declared as public.A source file can contain many classes but only one class can public which should contain your main().
Once we have declared our class,we can declare fields, methods and constructors in a class body.We will discuss what they are in next topics.


No comments:
Post a Comment