0% found this document useful (0 votes)
16 views9 pages

Class - MethodCalling - ObjectCreation by Kunal Sir Official

Notes

Uploaded by

Gopal Nanne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Class - MethodCalling - ObjectCreation by Kunal Sir Official

Notes

Uploaded by

Gopal Nanne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

by Kunal Sir

What Is Class?
1. Class is a different type of logical entity.
2. It is used to design template or blueprint.
3. With class keyword we can design class in java.

There are two Types of class: -

1) Inbuild Class: -
This type of classes predefined by the java community.

For Example:

System, String, Scanner class etc.

2) Custom Class: -
This type of classes design from our own purposes.

For Example:

public class A public class Student

{ {

} }

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Points To Be Remember Before Creating Class in Java: -

Point 1: - When class is public then class name and file name must be
same.

For example: -

Java Source Filename  A.java

Java Byte Code File  A.class

public class A

{ Compile Command: javac A.java

Running Command: java A

Point 2: - When class is not public then class name and file name can be
different.

For example: -

Java Source Filename  A.java

Java Byte Code File  B.class

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

class B

{ Compile Command: javac A.java

Running Command: java B

Basic Java Program: -

A.java
Create a class named "A":

public class A

public static void main(String args[])

System.out.println(“Welcome to CJC”);

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Compile: javac A.java

Run: java A

Output: Welcome to CJC.

What are Methods?


A method is a block of code or collection of statements or a set
of code grouped together to perform a certain task or
operation.
It is used to achieve the reusability of code.

Types of Methods: -
There are Four types of methods:

Simple Method: -
Syntax: - AccessModifier ReturnType MethodName()

For example: -

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

public void m1()

Simple Parameterized Method: -


Syntax: - AccessModifier ReturnType MethodName(parameter)

For example: -

public void m1(int x)

Return Type Method: -


Syntax: - AccessModifier ReturnType MethodName()

return --;

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

For example: -

public int m1()

return 10;

Return Type Parameterized Method: -


Syntax: - AccessModifier ReturnType MethodName(int x)

return x;

For example: -

public int m1(int x)

return x;

Important Things to Know: -


Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

There are 2 important things to know—

Object Creation: -
Syntax: -
ClassName referencevariable = new ClassName();
For example: -
A a = new A ();
Where:
A -- ClassName
a -- ReferenceVariable / Object
new -- Keyword

Method Calling: -
Syntax: -
referencevariable.methodName();
For example: -

a.m1();
Where:
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

a -- ReferenceVariable / Object
m1 -- Method Name (m1—Method1)

Basic Program based on method calling and object creation:


public class A

public void m1()

System.out.println(“M1 method”);

public static void main(String args[])

System.out.println(“Main Method Start”);

A a=new A(); //Object Creation

a.m1(); // Method Calling

System.out.println(“Main Method End”);

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Compilation Command: - javac A.java

Running Command: - java A

Output: - Main Method Start


M1 Method
Main Method End

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204

You might also like