Class and Objects

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Class and Objects-

Class is the template or blueprint from which object is created and Object is the
instance of class.
Class can only be accessed from outside via its instance

Why we write the class?


Because you want to design the something in the class. Class contain the
variables and methods.

Note- We rarely used int and float in class, generally we prefer string in java for
storing the value of variables.

1. Built in classes in java


Built-in classes in Java are the classes which come bundled within
predefined packages in Java. A few of the majorly used built-in classes are:

i. java.lang.String
ii. java.lang.System
iii. java.lang.Exception
iv. java.lang.Object
v. java.lang.Class
vi. java.util.Date
vii. java.util.HashMap
viii. java.util.ArrayList
ix. java.util.Iterator
x. java.lang.Thread

2. User defined or custom classes


As the name suggests, a custom or user-defined class is a class that is
created by a user. It will contain the class members or variables as defined by
the user.

How to create the class

Syntax or skeleton of java class

<Access specifier> class <Class name> {

Class body here

}
Java class is generally consists of following elements as

1. Variable
They are declared within the body of the class. The general
syntax to declare a class variable is given below:

Example-
public class Student {

public int a=10;


}

where public is access specifier


int is data type
a is field or variable name
10 is value

2. Methods-
A method in Java is a collection of a statement which
determines the behavior of a class object.

3. Constructor
4. Blocks
5. Nested classes
Class within other class called as Nested class in java.

Rules for class-

1. A Java class must have the class keyword followed by the class
name.
2. The class name must start with a capital letter and if you are using
more than one word to define a class name, every first letter of the
words should be made capital. Example- StudentData
3. There should not be any spaces or special characters used in a
class name except the dollar symbol($) and underscore(_).
4. A Java class can only have public or default access specifier.
5. It can extend only one parent class. By default, all the classes
extend java.lang.Object directly or indirectly.
6. A class may optionally implement any number of interfaces
separated by commas.
7. The class’s members must be always declared within a set of curly
braces {}.
8. Class containing the main() method is known as the Main class as it
will act as the entry point to your program.

What is the object in java?


An object in Java is the real-world entity which has its own state and behavior.
A Java program can have as many objects as required. An object in Java typically
contain following things:

1. State: This is represented by the attributes and properties of an object.


2. Behavior: This is defined by the methods of an object, In other words, its
functionality.

For Example, Mobile is an object. Its name is Samsung; color is black known as its
state. It is used for calling, texting message so its behavior.
When we create an object (instance) of class then space is reserved in heap
memory.

Let’s understand with the help of an example.

FirstProgram firstProgram = new FirstProgram();

Where firstProgram is the object of FirstProgram class.

Example- Program to display the message on console.

public class FirstProgram {

public static void main (String[] args) {

System.out.println("Welcome to Java Programming..");


}
}

Output
Welcome to Java Programming..

You might also like