Java – Class, Objects, Variables, Data Types and Packages

Java Class :

What is a Java Class ?

Class is defined as the template which is used to create objects or methods which is used for common purpose.

Since Java is purely and Object-Oriented Programming language, Java template class is used to create many number of objects.

How class can be declared in java program?

Class is declared as java-keyword class followed by ClassName with open and closed braces.

class ClassName{

public static void main(String args[])

An access specifier may or may not be applied in front of the java-keyword class.

What is an access specifier ?

Access specifier is a java-keyword which determines the accessibility of the class, methods and objects.

there are 4 types of access specifiers

1. public

2. private

3. protected

4. default

1. public

It is a java-keyword which allows the accessibility of class, objects, methods, variables and other members from anywhere, since its mentioned as public class.

public class ClassName{
        public static void main(String args[])

Above class is accessible from anywhere inside the package

2. private 

It is a java-keyword which allows the accessibility of class, objects, methods, variables and other members only inside the public class.

Since it is a private class other public classes cannot access this private class.

But private classes can be accessible by when a public class have inner class as private class

public class ClassName{
        public static void main(String args[])
        private class ClassNameOne {


3. Protected

Its a java keyword which allows the accessibility class, objects , methods variables and other members from any class inside the same package.

Classes cannot be declared as protected.

This access modifier is generally used in a parent child relationship.

class 1 :

package abcpackage;
public class ClassOne {

   protected int add(int a, int b){
 return a+b;

class 2 :

package xyzpackage;
import abcpackage.*;

class ClassTwo extends ClassOne{

   public static void main(String args[]){
   ClassTwo obj = new ClassTwo();
   System.out.println(obj.add(11, 22));

4. Default

When no access specifier is mentioned by default java considers as public (access specifier)

Java Package :

What is a java package ?

Java package is the namespace which is declared to organise the set of related classes and its interfaces.

We have to define the package name on the top of the .java file.

There are two types of packages

1) User defined package:

This is user-defined package, so we can create the package directory 

com.test.common.MyJavaFiles .

Then create an file inside the directory with the first statement being the package names.

package com.test.common.MyJavaFiles;
public class ExampleClass
    public void getVal(String s)

import com.test.common.ExampleClass;
public class PrintName
   public static void main(String args[])
      String name = "Abdul Kalam";
      ExampleClass obj = new ExampleClass();

Output :

Abdul Kalam

The package created by us is called user-defined package, which is also called as custom package.

package packageName;

public class ClassName { 
 public static void main(String args[])


2) Built-in package:

Java already have some pre defined package also called as built-in package like*, java.lang.* etc

a) java.lang:

Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.


Contains classed for supporting input / output operations.

c)  java.util:

Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations.

d)  java.applet:

Contains classes for creating Applets.

e)  java.awt:

Contain classes for implementing the components for graphical user interfaces (like button , menus etc).


Contain classes for supporting networking operations.

if we want to use the java build-in package, then we have to import the package name.

package packageName;
import java.lang.*;

public class ClassName{
public static void main(String args[])


So Class is the main thing in Java, and there are common types of class listed below.

1. Wrapper Class
2. Mutable Class
3. Abstract Class
4. Final Class
5. Anonymous Class
6. Input-Output Class
7. String Class
8. System Class
9. Network Class

Note : Above classes will be explained in detail later on in this tutorial series.

Java Objects :

Java class is the template or blueprint used for creating objects.

Objects are also called as instance of the class which can be combination of variables, methods and other members.

Objects can be created by using the java-keyword “new“.

public class ClassName 
    String name = "Sample Name";
    public static void main(String[] args) 
        ClassName obj = new ClassName();

Output :

Sample Name

We can also create multiple objects inside the class

public class ClassName
    String name = "Sample Name";
    int age = 10;
    String sex = "Male"
    String location = "India";
    public static void main(String[] args)
        ClassName obj1 = new ClassName();
        ClassName obj2 = new ClassName();
        ClassName obj3 = new ClassName();
        ClassName obj4 = new ClassName();

Output :

Sample Name




What is class and object ?

Class and Object are the most important terms in Object Oriented programming.

What is class ?

1. Class is the basic blueprint or protocol which determines the behaviour of the object and its action.

2. Class may contain one or many objects which may be similar or different.

3. Class is usually declared by using the class keyword.

4. Memory is not allocated for the class when its created.

5. Class may of parent class, child class etc.

What is an Object ?

1. Objects are created by using the “new” keyword.

2. Objects determines the entire behaviour of the class.

3. We can create many objects based upon the users requirement.

4. Memory is allocated when the object is created.

Java Variable :

What is a variable ?

Variable is the one which is liable to change, it prefers changing from one value to another.

Variables in programming language have same qualities as mentioned above.

What is a variable in programming language ?

Variable is simply a value which have the ability to change itself depending on the conditions passed to the program.

What is variable in Java ?

In Java, variable is the Name given to the allocated memory location inside the memory, and variables should be declared with predefined attribute called data-types.

What are the types of variables in computer language ?

There are classified in two types

1. Dependent variables
2. Independent variables.

What is dependent variable ?

A variable whose value depends upon other independent variables.

sometimes its called the output variable.




now c=40, The value of c depends upon the values of a and b.

Now c is the dependent variable.

What is Independent variable ?

Its is the independent variable whose values is not dependent upon other variables.

Its an individual variable that determines the value of dependent variable.




now c=40, the value of c is determined by a and b.

variable a and b are independent variables.

What are the types of variables in java ?

There are three types of variables in Java:

1. Local Variables

2. Instance Variables

3. Static Variables

Local variable in java ?

A variable defined within a block / method / constructor is called local variable.

We can use the variable only within that method / block/ constructor . Other methods in the class aren’t even aware that the variable exists.

public class HelloApp

    public static void main(String[] args)

        String helloMessage;
        helloMessage = "Hello, World!";

Output :

Hello, World!

Instance variable in java ?

Instance variable is mainly used by Objects to store their states.

It is a non-static variables and are declared in a class outside any method / constructor / block of code.

Instance variables are created when an object of the class is created and destroyed when the object is destroyed.

Note : We can use access specifiers for instance variables, and If we do not specify any access specifier then the default access specifier will be used for these variables.

class Test {
        public String Name;    
        private int age;      

Static variable in java ?

Static variables in java is defined with static keyword.

when a variable is declared as static, then the copy of variable is created and shared among all objects in the class.

Its also called as global variables allows to share the same static variables in all instances of the class.

Advantage of static variables ?

The main advantage in declaring static variables is memory allocation happens once when the class is loaded in the memory, so it saves the memory.

class TestExample{
  static String name;
  static int age;
  static void PrintOut(){
      System.out.println("Name is: "+name);
      System.out.println("Age is: "+age);
  public static void main(String args[])
          age = 83;
          name = "Abdul Kalam";

Output :

Name is: Abdul Kalam

Age is: 83

Java Date types :

What is data type ?

Data type is an attribute that a data value can have.

It defines the type of value that a data can hold in a programming language.

It also tells the characteristics (predefined attribute) of the data value to the compiler for the variables.

There are 8 types of datatypes

How many bits in a byte ?

Group of 8 bits together to make 1 byte.

byte is a collection of 8 bits.

1. byte

Byte is the defined as the smallest integer data-type.

Byte variables are declared by use of the java-keyword byte.

byte a = 100;

Range : It has a minimum value of -128 and a maximum value of 127 (inclusive)

2. short

Short variables are declared by using the java-keyword short.

The short data type is a 16-bit type.

short a = 2300;

Range : It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive)

3. int

Int variables are declared by using the java-keyword int.

It is a signed 32-bit type.

int a = 12345;

Range : has a range from –2,147,483,648 to 2,147,483,647 (inclusive)

4. long

Long variables are declared by using the java-keyword long.

It is a signed 64-bit type.

It is used for very large number variables.

long a = 123456789;

Range : It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

5. float

Float variables are declared by using the java-keyword float.

The type float specifies a single-precision value that uses 32 bits of storage.

Float type is a decimal value type and syntax is denoted by adding “f” after the initialing the variable.

float a = 12.34f;

Range : It have the range from 1.4e-045 to 3.4e+038 approx.

6. double

Double variables are declared by using the java-keyword double.

The double keyword, uses 64 bits to store a value.

Its a higher level decimal value type and syntax is denoted by adding “d” after the initialing the variable.

double a = 12345.678d;

Range : It have the range from Approx range 4.9e-324 to 1.8e+308 appox.

7. boolean

Boolean variables are declared by using the java-keyword boolean.

Boolean data type has only two possible values: true and false.

It is a signed 1-bit type.

boolean a = true;

boolean b = false;

8. char

Char variables are declared by using the java-keyword char.

The data type used to store characters is char.

It is a single 16-bit Unicode character.

char a = ‘A’;

Range : It have minimum value Unicode 0 and maximum value Unicode 216-1

Byte, Short , Int , Long

SNoData Typessize(bites)Range
1Byte1+127 to -128
2.Short2+32767 to -32768
3Int4+2147483647 to -2147483648

Float and Double

SNoData Typessize(bites)RangeNumber of Decimal places
1Float4+2147483647 to -21474836488


SNoData Typessize(bites)RangeNumber of Decimal places
1Char2+32767 to -327688