Core Java(Part-2)

Content

. Package statement

. import statement

. Class declaration

. Methods

. Variables

Package

In java program can execute without package. Everything in java world is represent in form of entity/classes. How we write package in java program. Spark2.0 is a class in java inside Spark2.0 there is a class with a name of Java inside Java class there is another class named as Intro. So in lame language you can say folder inside folder to understand it. Here in java we use class in place of folder.

πŸ“¦ TravelBag (Package) β”œβ”€β”€ πŸ‘• TShirt.java β”œβ”€β”€ πŸ‘– Jeans.java └── πŸ‘Ÿ Shoes.java

  • The suitcase = the package

  • The clothes = different Java classes

  • You group related things together so they’re easy to find, manage, and reuse

    βœ… Why Use Packages?

    • Organize code (like organizing clothes in different bags)

    • Avoid name conflicts (e.g., two classes named Test can exist in different packages)

    • Make code reusable and readable

Example:- package travelbag/Tshirt/Jeans/Shoes;

import

"Jab tumhare paas koi item nahi hota, toh dusre dukaan se manga lete ho. Java mein bhi wahi hota hai!"

In java program we use import for import package, class which is not a part of your program which you want to use in your program by using import keyword you can use all thing that you import. In common language you import something from market. Similarly in java we bring other packages and classes into our code by using import keyword so that we can use all the method/function of import class.

import java.util.Scanner;

here in this we import Scanner class from the java.util package, so that we use its method/function for taking input from the user

Class declaration

In java we always declare class first. All the thing written inside the class (keyword) like variable, methods.

Bhai, soch class ek blueprint hai... Jaise ek design banate ho car ka, waise hi Java mein design banta hai object ka.

Tum bole: "Main ek banda design kar raha hoon β€” uska naam Student hai, uske paas naam, roll no. hoga, aur wo padhai karega."

class Student {

    String name;

    int rollNo;

    void study() {

        System.out.println("Studying hard boss!");

    }

}

Methods

In methods we write all the task that we have to perform

"Method declaration" = Java ko batana ki
β€˜bhai ek kaam karne wala function hai, uska naam ye hai, aur ye ye kaam karega.’

Here how we declare method in java?

(MethodName)helloWorld(){

}

Lame Analogy: Class = Burger Shop

Method = Banane ka tarika (recipe)

Toh method declaration =

"Boss, burger banana hai? Toh yeh steps hain!"

void makeBurger(){

    System.out.println("Bun + Patty + Cheese = Burger πŸ”");

}

Variable

Variable = A box that stores data. Just like a lunchbox holds food β€” a Java variable holds a value.

int age = 20;

"Ok bhai, ek age naam ka dabba bana liya,
usmein number type ka 20 rakha hai. Done!"

Variable = A named container that stores a value so it can be used later.

0
Subscribe to my newsletter

Read articles from Khelendra Kumar Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Khelendra Kumar Thakur
Khelendra Kumar Thakur