2 Java Core
2 Java Core
A class is a blueprint/template that defines the state and behavior of objects. It is the logical entity that wraps all
our code into a single unit and structures the code. In Java, we cannot compile our code without a class. Hence,
we must write the code only within the class body surrounded by Curly braces.
The unique characteristic of a class is that a user can define their data members (Variables with Data Types) and
methods, and we can access them by creating instances of the class
By Deserialization.
Class Object
1. It is a blueprint or template for creating objects. 1. It is an instance of a class which has a state and behavior.
2. A Class characterizes a group of similar objects 2. An object represents a real world entity with some attributes and
having same properties. features.
3. In java, Class is only loaded in memory during 3. On the other hand, Objects are allocated memory dynamically as they
compile time for reference. are created during runtime .
4. A Class represents a logical entity which binds data 4. An Object is a physical entity which provides us means to access the
and methods i.e. our code into a single unit. members of a class.
5. A Class is declared only once. We declare a class 5. Objects can be created more than once depending on the requirement.
using keyword class. We declare objects using new Keyword.
Simply put, Variables are helpful to programmers to handle data that can be manipulated by the program during
execution as well. Variables in java are Strongly typed; hence they all must have a datatype followed by an
identifier.
Variable has a name that is used to identify it. The name of the variable is known as the variable’s identifier.
There are some naming conventions that you need to follow while declaring a variable.
Note: It is mandatory to initialize the local variables; otherwise, the compiler will throw an error about it.
Moreover, we cannot have Keywords like public, private, static associated with Local Variables.
Instance variable
A variable declared at the class level but outside a method or a block is an Instance variable. It is not mandatory to
initialize instance variables. Instead, an Instance variable in Java is initialized when an instance of the class or
an object is created.
All instance variables will be, by default, initialized by JVM. While allocating space for an object, a slot for each
Instance Variable is also created. Thus, we can say Instance Variables are present in memory until the object’s
lifetime and are visible to all methods and blocks in the class.
We can use Keywords and Access Modifiers with Instance Variables to restrict the visibility of the variables. Let us
understand the use of instance variable with example.
To access the value in the main method, we had to create the object or instance of the Class InstanceDemo; then,
we used the dot operator with the object name to access the variable.
If we had accessed it directly without creating the object, the compiler would throw an error: non-static variable g
cannot be referenced from a static context. This error means that the main method is static, and we cannot access a
non-static variable from a static method. It is mandatory to instantiate the class in this case.
Static variable
A variable that is declared as static and refers to shared property for all class objects is known as a Static variable.
Static variables are also class-level variables that can be declared in a method but only in a Static Method.
We cannot declare static variables inside a Static Block.
Static Variables in Java make the program memory efficient as static variables get memory allocation only once
during class loading. In simple words, all objects of a class will share the same static variable or properties.
Even if we create multiple objects or instances, static members are created only once. The static keyword denotes
that a property belongs to the whole class rather than a method. Like Instance variables, Static members are also by
default initialized by JVM.
Suppose we want to store information that will be the same for all Student Objects like their School Name, so in that
case, we can use a Static variable so that every object shares the same variable. If we want to temporarily store any
value related to the object for any operations, we can use Local Variables.
keyword
Final variable
If you make any variable final then you are not allowed to change its value later.It will be constant.If you try to
change value, then compiler will give you error.
Final Class
If you declare a class final, no other class can extend it.
Static keyword
Variable
Method
Block
Nested class
Static variable:
Static variable belongs to a class not to a object.
Static variable are initialized only once in class area at the time of class loading
All objects share single copy of static variable
You don’t need to create object to access static variable.You can directly access it using class name.
Static variable can be accessed by instance java methods also.
If you create a class with two variables, one static, one non static.Non static variable of that class (objects created
from that class) gets its own version of that variable. But with the static variable, it belongs to the class, and there is
only one. Although any object from that class can reference it.
Static Method:
Static block:
The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the
JVM
Static class:
In java,you can define a class within another class. Such a class is called a nested class. The class which enclosed
nested class is known as Outer class. In java, we can’t make outer class static.
1. It is the built-in Data Type that is predefined by Java 1. It is the user-defined datatypes and not predefined by
itself. Java except the String Class.
3. It does not provide any inbuilt methods to invoke for 3. We can invoke or call methods, even define our own
performing operations on the data. methods to perform operations.
4. It always has a value to it and cannot be null. 4. It can have a null value.
you can not access method, variable or class outside of the package.