0% found this document useful (0 votes)
2 views

Language Fundamentals

The document outlines Java coding standards, emphasizing the importance of naming conventions for classes, interfaces, methods, variables, and constants to enhance code readability and maintainability. It also explains different types of variables (instance, static, and local), access modifiers, and constructors, including their rules and purposes. Additionally, it discusses the use of the 'this' keyword for referencing current class instances and invoking methods and constructors.

Uploaded by

applepro1682
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Language Fundamentals

The document outlines Java coding standards, emphasizing the importance of naming conventions for classes, interfaces, methods, variables, and constants to enhance code readability and maintainability. It also explains different types of variables (instance, static, and local), access modifiers, and constructors, including their rules and purposes. Additionally, it discusses the use of the 'this' keyword for referencing current class instances and invoking methods and constructors.

Uploaded by

applepro1682
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Language Fundamentals

~Ganesh Jadhav
Java Coding Standards

• Whenever we are writing java code it is highly recommended to follow coding


standards whenever we are writing any component it’s a name should reflect the
purpose of that component.
• The main advantage of approach is the readability & maintainability of the code
will be improved.

Coding standards for classes


1. usually class name are nouns
2. Should start with uppercase characters & if it contain multiple words every inner
word should start with uppercase characters. Ex. String, StringBuffer
Coding standards for Interface
• Usually, interface names are adjective.
• Should starts with uppercase characters & if it contain multiple words then every
inner word always start from uppercase characters.
Ex. Runnable, Serialization, Comparable

Coding standards for method name


• Usually, method name are either verbs or verb noun combination.
• Should starts with lowercase alphabets symbols & if it should contain multiple
words then every inner word start with uppercase characters(CamelCase
Convention)
Coding standards for variable
• Usually, variables name are noun & always should start with lowercase alphabets
symbol.
• If it contains multiple words then every inner word should start with uppercase
character(CamelCase convention) Ex. Name, age, salary, mobile number

Coding standards for constants


• Usually constants name are nouns & should contain only uppercase characters & if
it contains multiple words then this words are separated with underscore symbol.
• Ex. MAX_VALUE, MAX_PRIORITY, MIN_PRIORITY
• Usually, we declared constants with public static & final modifiers.
Types of Variable
• Primitive Variables
• Can be used to represent primitive values.
• Int x = 10;
• Reference Variables
• Can be used to refer object
• Ex. Student s = new Student();
• Based on positive of declaration & behavior variable are divided into three type
1. Instance Variable
2. Static Variable
3. Local Variable
Instance Variable

• 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

• The static keyword in Java is used for memory management mainly.


• We can apply static keyword with variables, methods, blocks and nested classes.
• The static keyword belongs to the class than an instance of the class.

 Static Variable (also known as a class variable)


 Static Method (also known as a class method)
 Static Block
Static Variable (also known as a class variable)

• 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

• Is used to initialize the static data member.


• It is executed before the main method at the time of class loading.
• Before java version 1.7 it was running without main method but since java version
1.7 it’s not running without main method.
Local Variable

• 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

• The access modifiers in Java specifies the accessibility or scope of a


field, method, constructor, or class.
• We can change the access level of fields, constructors, methods, and class
by applying the access modifier on it.
• Private: The access level of a private modifier is only within the class.

• 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.

• Public: The access level of a public modifier is everywhere. It can be accessed


from within the class, outside the class, within the package and outside the
package.
Understanding Java Access Modifiers

Access within class within package outside package by outside package


Modifier subclass only

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y
Constructor in Java

• In Java, a constructor is a block of codes like the method.


• It is called when an instance of the class is created.
• At the time of calling constructor, memory for the object is allocated in the
memory.

Rules for creating Java constructor


• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized
• It is called constructor because it constructs the values at the time of object
creation.
• It is not necessary to write a constructor for a class, because java compiler creates
a default constructor if your class doesn't have any.
• It calls a default constructor if there is no constructor available in the class.
• In such case, Java compiler provides a default constructor by default.
• It is a special type of method which is used to initialize the object.
• Every time an object is created using the new() keyword, at least one constructor
is called.
There are two types of constructors in Java

• Default constructor (no-arg constructor)


• Parameterized constructor

Default constructor (no-arg constructor)


• A constructor is called "Default Constructor" when it doesn't have any parameter.

Q) What is the purpose of a default constructor?


• The default constructor is used to provide the default values to the object like 0, null,
etc., depending on the type.
2. Parameterized Constructor
• A constructor which has a specific number of parameters is called a parameterized
constructor.

Why use the parameterized constructor?


• The parameterized constructor is used to provide different values to distinct objects.
However, you can provide the same values also.

Private Constructor in Java


• In the same way, Java also allows us to create a private constructor.
• We can declare a constructor private by using the private access specifier.
• If a constructor is declared private, we are not able to create an object of the class.
• Instead, we can use this private constructor in Singleton Design Pattern.
Rules for Private Constructor

• It does not allow to create an object outside the class.


• If a class has a private constructor and when we try to extend the class, a compile-time
error occurs.
• If all the constant methods are there in our class, we can use a private constructor.
• If all the methods are static, then we can use a private constructor.
This keyword in Java

In Java, this is a reference variable that refers to the current object.

Usage of Java this keyword


• this can be used to refer current class instance variable.
• this can be used to invoke current class method (implicitly)
• this() can be used to invoke current class constructor.

1) this: to refer current class instance variable


• This keyword can be used to refer current class instance variable.
• If there is ambiguity between the instance variables and parameters, this keyword
resolves the problem of ambiguity.
2) this: to invoke current class method
• You may invoke the method of the current class by using this keyword.
• If you don't use this keyword, compiler automatically adds this keyword
while invoking the method.

3) this() : to invoke current class constructor


• The this() constructor call can be used to invoke the current class constructor.
• It is used to reuse the constructor. In other words, it is used for constructor
chaining.

You might also like