# Classes, Objects and Methods in Java with Programming Example

#### In this chapter you will learn: 1. What are classes in java and how to create it?
2. What is objects and how to initialize it?
3. What is methods and how to create it?
4. Programming Example
This is the very important chapter in java which enables you to make your own class, objects and methods. As you know java is object oriented programming language which means you can write program once and use it many times. The place where you write programming logic is class and when you use your program multiple times it is objects.

#### What are classes?

Classes are the blueprint of your program. It is the place where you define variables, methods, constructors, blocks, interfaces and program logic. Once classes are completed you can use it many times by creating its alias name or objects.

#### How to define class?

You can define class with class keyword as follow:

```  public class MyClass

```

What is object and how to initialize it?

An object is an alias of the class which represents class behavior. A class may have many objects with different names. You can define objects as follow:

```  Class_Name object_name=new Class_Name();

```

#### What is method or function?

A method of function is a basic building block of program which contains set of code and referred to by a name. A method is created inside class and can be accessed anywhere in program directly by name or followed by its class name.

How to define method?

It is very easy to define method in java. You can write method as follow:

```public void myname()
{
//code block
}

Or

public String myname()
{
//code block
Return String_Value
}

```

OK. It’s enough for theory. Let’s understand all these concepts using programming example. In the following program we will create classes, methods and objects.

#### Programming Example:

```class CreatingClass
{
public static void main(String[] args)
{
int result;

//Creating Calculator Object
Calculator calc=new Calculator();

//Creating PrintData Class Object
PrintData pdata=new PrintData();
//Calling print() method
pdata.print(result);
}
}

//Defining Class Calculator
class Calculator
{
public int Add(int num1, int num2)
{
System.out.println("Calculator Class Started...");
return num1 + num2;
}
}

//Defining Class PrintData
class PrintData
{
public void print(int value)
{
System.out.println("PrintData Class Started.. ");
System.out.println(value);
}
}
```

Output

D:\JavaProgram>javac CreatingClass.java

D:\JavaProgram>java CreatingClass
Calculator Class Started…
PrintData Class Started..
30

D:\JavaProgram> __

Explanation:

In the above programming example we have done the following job.

1. Created 2 separate class `Calculator` and `PrintData`.
2. `Calculator` class have a method `Add(int num1, int num2)` which requires 2 integer number to process and it returns addition of both number.
3. `PrintData` class have a method `print(int value)` which requires an integer value and print it on screen.
4. In the main method I have created object of `Calculator` Class and send 2 integer value for processing.
```Calculator calc=new Calculator();
```PrintData pdata=new PrintData();
{"ft_lang_label":"__label__en","ft_lang_prob":0.7942595,"math_prob":0.7201059,"size":3189,"snap":"2021-43-2021-49","text_gpt3_token_len":676,"char_repetition_ratio":0.14850864,"word_repetition_ratio":0.012396694,"special_character_ratio":0.21950455,"punctuation_ratio":0.13945578,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9734261,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-16T04:45:56Z\",\"WARC-Record-ID\":\"<urn:uuid:03dca26e-7219-4485-861c-f62cd3e6bbcf>\",\"Content-Length\":\"45913\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:db881930-f15c-4708-8cbe-6c7690930710>\",\"WARC-Concurrent-To\":\"<urn:uuid:fbabdd56-c337-47b1-b6ce-4c6ffb8da700>\",\"WARC-IP-Address\":\"172.67.158.162\",\"WARC-Target-URI\":\"https://www.mainjava.com/java/core-java/classes-objects-and-methods-in-java-with-programming-example/\",\"WARC-Payload-Digest\":\"sha1:5SA2QOLYCNJFHQSEOLTSBIVS3QOU7RTB\",\"WARC-Block-Digest\":\"sha1:NPY6TJPIJBMMDXCGV356LOQHYSVFQJUT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323583423.96_warc_CC-MAIN-20211016043926-20211016073926-00218.warc.gz\"}"}