In Java , classes , objects and packages are basic fundamental concepts of object-oriented programming language.
Class:
A class is a template or blueprint for creating objects . Class defines the structure(fields/data members ) and behavior(methods ) that objects of that class will possess. A class doesnot occupy memory for its attributes and methods until an object is created from it .
The below Java code demonstrates the basic use of class in Java . It defines a Gift class with two instance variables ( score and giftName) , creates an object g1 and prints values of g1 .
class Gift{
int score;
String giftName;
//Added constructor to initialize both above fields
public Gift (int score , String giftName ) {
this.score=score;
this.giftName=giftName;
}
}
public class Main {
public static void main( String args[] ) {
// Creating Gift object using the new constructor
Gift g1 = new Gift (95 , “$300” );
System.out.println (g1.score);
System.out.println(g1.giftName);
}
}
Output
95
$300
Properties of Java Classes :
- Class is just a blueprint or template from which objects are created. It is not a real -world entity .
- A class in Java can contain Data members , methods , a Constructor , Nested classes etc .
- A class itself doesnot occupy memory for its attributes and methods untill an object is instantiated .
Java Objects
Java objects are instance of a class which are created to use the methods and attributes of a class .
An object in java is the physical as well as logical entity.
A java program can creates many objects , which are interacted by invoking methods .
An object consist of :
- Identity : Identity gives a unique name to an object and enables one object to interact with other objects.
- State: State of an object is represented by attributes of an object which also reflects the properties of an object
- Behavior : Behavior of a object is represented by the methods of an object which also reflects the response of an object with other objects .
An object will create memory at the time of execution. So object is known as runtime entity .
Every objects allocates memory inside heap memory which is a part of JVM memory .
Syntax:
className <refvariableName>= new className();
Example :
class Gift {
int score;
String giftName;
long phoneno;
void display () {
System.out.println(“Displaying the gift holder details : “);
System.out.println(“Score is : “+ score);
System.out.println(“Gift is: “+ giftName);
System.out.println(“Phone no of the gift holder is : ” +phoneno);
}
}
class DemoObject {
public static void main ( String args[] ) {
//Creating an object of a class Gift
Gift g1=new Gift ();
g1.score=95;
g1.giftName=”$300“;
g1.phoneno=111111111;
g1.display();
}
}
OUTPUT:
Displaying the gift holder details :
Score is : 95
Gift is : $300
Phoneno of the gift holder is : 111111111
In the above example Gift class contains states which are score, giftName , phoneno and behavior which is display() method.
We created an object of Gift class inside main class DemoObject using the syntax Gift g1= new Gift () ;
After creating the object reference g1, we have assigned values to the variables of the class Gift and the called the display() method using object reference g1 .
JAVA PACKAGE:
Java package is a mechanism which organize related classes , interfaces , enumerations and annotation types into a single namespace . Packages are used in Java to avoid name conflicts and for better maintainance.
Java packages are used for:
- Java package prevent naming conflicts by allowing classes with same name to exist in different packages
- Java package provide controlled access for protected members that are accessible by subclasses and within the same package.
- Java package make it easier to organize , locate and use classes , interfaces , annotations and other components.
By grouping related classes , interfaces into packages , Java promotes data encapsulation , making code reusable and easier to manage . If we want to use another class properties within a different package class , then we have to just import the package that contains the required class.
There are two primary types of packages in Java:
- Built-in Packages
- User-Defined Packages
- Built-in Packages( Java API Packages ) :
Built-in packages are pre-defined packages provided by the Java API , which offers a wide range of functionalities.
Some of the built-in packages are :
java.lang : This package contains fundamental classes like String , Math and Object
java.io : This io package contains classes for input and output operations.
java.util : This util package contains classes like ArrayList , HashMap and date/time utilities .
java.net : This package contains classes for networking .
javax.swing : This package is used for GUI development .
2. User-defined packages :
These user -defined packages are created by developers to organize their custom classes and interfaces within their projects .
How to Use Packages :
- Declaring a Package : You can declare a package at the top of a Java source file using the package keyword, followed by the package name.
package com.java.firstproject;
public class Lab{
//……
}
- Importing packages/Classes : To use classes or interfaces from other packages :
1. Import specific classes : import com.java.anotherpackage.AnotherClass;
2. Import all classes from a package : import com.java.anotherpackage.* ;
Naming Convention :
Java package naming conventions are designed to ensure uniqueness and prevent naming conflicts , basically in large projects and when using third-party libraries .
Naming convention principles
:
- All Lowecase : packages names are always written in all lowercase letters to differentiate them from class or interface name.
- Reversed Internet Domain Name : The recommended practice to start a package names for organizations is to start with their reversed internet domain name .
Example : If a company’s domain is company.com , their package names would start with com.company.
Further components can be added to reflect internal organization, project names or modules (e.g., com.company.firstproject.myclass).
- No Underscore or Special Characters : Avoid using special characters or underscore in package names.
- Hierarchial Structure : The dot (. ) in a package name represents a hierarchial structure , corresponding to directories and subdirectories in the file system where the class files are stored . For example , com.company.firstproject would corresponds to a directory structure like com/company/firstproject.
- Java Language Packages : Packages within the java language itself begin with java. or javax.
