You are on page 1of 18

Classes and

Methods in
Java
Introduction

Java is an object-oriented programming language. Everything in java is


associated with classes and objects, along with its attributes and
methods. For example, in real life, a car is an object. The car has
attributes, such as weight and color, and methods, such as drive and
brake.
Classes
A Class is like an object constructor, or a "Blueprint" for creating
objects.
Creating a Class
To create a class, use the keyword
Class: public class Main {
int x = 4;
}
Here, we have created a class named
Main.
Creating Object of a
Class
To create an object of the class Main, specify the class name, followed by the
object
name, and use the keyword new :
public class
Main{ int x =4;
public static void
main(String[]
args)
Main myObj = new Main();
Creating Object of a
Class
Access Modifiers
The access modifiers in java specifies the accessibility or scope of a class by applying
the
access modifier on it.
There are four types of Java access modifiers :
1. Private
2. Default
3. Protected
4. Public
Private and Default Access
Modifiers
Private : The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.

Default : The access level of a default modifier is only within the package. It cannot
be accessed from outside the package. If you do not specify any access level, it will
be the default.
Protected and Public Access
Modifiers
Protected : The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child classes, it
cannot be accessed from outside the package.

Public : The access level of a public modifier is everywhere. It can be accessed


from within the classes or outside the class.
Access Modifiers – A General
Comparison
Multiple Classes in
Java
Yes, we can have multiple classes in same java file. But, there is one restriction over
here, which is that you can have as many classes in one file but only one public
class is allowed. If we try to declare 2 classes as public in the same file, the code
will not compile.
The reason being is you need to name the file with the name of the class which is
declared as public and we cannot have the same file with different names, and also
that public class should be containing the main method as the compiler will check
for the main method first. So, there is no chance to have two public classes in one
file.
If we want to access the methods, instances of the other classes we can just make
Multiple Classes in Java -
Program
The following program comprises of two classes: Computer and Laptop, both the classes
have their constructors and a method. In the main method, we create objects of two classes
and call their methods.
Here, we can also create objects in a method of Laptop class. When we compile the
program, two class files are created, which are Computer.class and Laptop.class. Its
advantage is we can reuse our .class file somewhere in other projects without recompiling
the code.
We can create as many classes as we want, but writing many classes in a single file isn't
recommended, as it makes code difficult to read. Instead, we can create a separate file
for every class. We can also group classes in packages for efficiently managing the
development of your application.
Methods
A method is a block of code which only runs when it is called. You can pass data
(parenthesis), into a method. They are used to perform certain actions, and they are
also known as functions.
Why use methods?
It is used to reuse code which means to define the code once, and use it many
times. A method must be declared within a class. It is defined with the name of
the method, followed by parentheses ().
Creating a
Method
To create a method -
public class Main {
static void
myMethod() {
// code to be
executed
}
}
myMethod() is the name of the method. Static means that the method belongs to the
Main class. It is not an object of the Main class. Void means that this method does not
Method Declaration in
Java
Calling a Method in
Java
Predefined Method - In Java, pre-defined methods are the methods that are already
defined in the classes. When we required any pre-defined method, we just call the
method by its name.
User Defined Method - To call a user-defined method, first, we create a method and
then call it. A method must be created in the class with the name of the method,
followed by parentheses (). The method definition consists of a method header and
method body.
To call a method in Java you can write the method's name, two parentheses () and a
semicolon; immediately after it.
Thank you

You might also like