Language Fundamentals
Language Fundamentals
~Ganesh Jadhav
Java Coding Standards
• In any programming language, the program needs identifiers for storing different
values that can be used throughout the program. These identifiers are variables.
• If the value of a variable is varied from object to object such type of variable are
called Instance variables.
• For every object separate copy of instance will be created.
• Instance variable should be declared within the class directly but outside of any
method/block/constructor.
• Instance variable will be created at a time of object creation & destroyed at the
time of object distraction.
• Hence the scope of instance variable is an exactly same as scope of object.
• Instance variable will be stored in a heap memory.
• An instance variable can be declared using different access modifiers available in
Java like default, private, public, and protected.
• The instance variable is initialized at the time of the class loading or when an
object of the class is created.
Features
• To use an instance variable an object of the class must be created.
• An instance variable is destroyed when the object it is associated with is destroyed.
• Instance variables are accessible inside the same class that declares them.
Static Keyword
• Static variable in Java is variable which belongs to the class and initialized only
once at the start of the execution.
• It is a variable which belongs to the class and not to object(instance ).
• Static variables are initialized only once, at the start of the execution.
• These variables will be initialized first, before the initialization of any instance
variables.
• A single copy to be shared by all instances of the class
• A static variable can be accessed directly by the class name and doesn’t need any
object
• Ex. Static int b = 10;
How to load files in java?
1. Start JVM
2. create & start main method
3. Locate Test.class file
4. Load Test.class file (Static variable creation)
5. execute main method
6. unload Test.class (Static variable destruction)
7. Terminate Main method
8. Shutdown JVM
• For static variables JVM will provide default value & we are not required
to perform initialization explicitly.
• Static variable will be stored in method area
• We can access static variables by object reference by class name but
recommended to used class name.
• Within the same class it is not required to used class name & we can access
directly.
Static Method
• Static method in Java is a method which belongs to the class and not to the object.
• A static method can access only static data.
• It cannot access non-static data (instance variables).
• A static method can call only other static methods and can not call a non-static
method from it.
• A static method can be accessed directly by the class name and doesn’t need any
object
Java static block
• A variable declared inside the body of the method is called local variable.
• You can use this variable only within that method and the other methods in the
class aren't even aware that the variable exists.
• A local variable cannot be defined with "static" keyword.
• Local variables are declared in methods, constructors, or blocks.
• Local variables are created when the method, constructor or block is entered, and
the variable will be destroyed once it exits the method, constructor, or block.
• Local variables are implemented at stack level internally.
• There is no default value for local variables, so local variables should be declared,
and an initial value should be assigned before the first use.
• The only applicable modifiers for local variables is final.
No. Local variables Instance variables Static variables
1. Variables declared within a An instance variable is Static variables are declared inside a
method are local variables. declared inside a class but class but outside of a method starting
outside of any method or with a keyword static.
block.
3. A local variable starts its The object associated with the The static variable has the same
lifetime when the method is instance variable decides its lifetime as the program.
invoked. lifetime.
4. Local variable is not accessible Instance variable has different Static variables only have one single
to all the objects of the class. copies for different objects. copy of the entire class.
5. Used to store values that are Used to store values that are Used for storing constants.
required for a particular needed to be accessed by
method. different methods of the class.
Access Modifiers
• Default: The access level of a default modifier is only within the package. It
cannot be accessed from outside the package. If you do not specify any access
level, it will be the default.
• Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it
cannot be accessed from outside the package.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Constructor in Java