Class and Objects are the one of the important concepts of the object oriented programming language. And java is incomplete without classes every thing we discussed in java is purely depends on class and object oriented.
Here in this article we are going to learn how to use class with example.
Definition:
A class is the template or blueprint from which objects are actually created. The class is created with the keyword followed by the name of the class. All objects are the instances of class.
Now let us consider an example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class classname { public static void main(string args[]) { ….............. ….............. } } |
Access Specifiers of the classes
- public
- protected
- default (not specified)
- private
If the class has access specifier as public then it can be accessed from anywhere..
And if it is default it is accessed from the same package only
Access Specifiers | Same Class | Same Package | Subclass | Other packages |
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no access modifier | Y | Y | N | N |
private | Y | N | N | N |
Y for Yes
N for NO
Points To Remember About The Class
-
A program is made up of one or more classes
-
Name of the file should coincide with the name of the class if that class is declared as public
-
Public indicates it is visible from any point of the program
-
Static is to call the method without creating an instance of the class
-
Void indicates, it does not return anything.
-
string args[] is to take the data from command prompt.
-
Java file should not contain more than one public class.
-
The data or variables defined within the class are called instance variables
-
Functions defined within a class are called methods.
- The class declaration and the implementation of the method should be within the same class
Recommended Articles on java
If you had any doubts or comments regarding this article let us know through comments.