What is a class in Java?

What is a class in Java?

What is a class in Java? A simple question to answer. Class is a basic unit of Java programming. A class in Java is a structure which contains programming constructs. Programming constructs are small pieces of code with which a program is built; like bricks, cement and sand are constructs to construct a building. The programming constructs are mainly constructors, methods and variables. So finally, to say, a class is made up of or embeds constructors, methods and variables, all doing a predefined task.

To access these constructs, we require a handle which points (refer) all these. This handle is known as object. So, it is necessary to create an object of the class to access these constructs. A class gives a boundary for the whole code. Java demands you to write any amount of code within its boundaries delimited by two braces like { and }.

Following gives a specimen of a class with a constructor, a method and some variables. An example for What is a class in Java?

What is a class in Java?Output Screenshot on What is a class in Java?

Employee emp1 = new Employee();

This is the style of creating Java object; on both sides class name Employee will be there. Object is emp1 and it is a handle to access constructor, methods and variables in the class. Observe, all the code is written within the braces of the class declaration.

For more Examples and more Explanation refer the following links of this same Web site..

1. Java Example Compilation Execution
2. Java Local Instance Variables
3. Using Variables from Methods
4. Data Binding Data Hiding Encapsulation

After going through the above links, slowly go deep into this site step-by-step. Better have help from your friend knowing Java or a good Lecturer from a good Institute, so that learning will be faster and orderly.

Leave a Reply

Your email address will not be published. Required fields are marked *