Java
Java
An entity that has state and behavior is known as an object e.g., chair, bike,
marker, pen, table, car, etc. It can be physical or logical (tangible and
intangible). The example of an intangible object is the banking system.
Object Definitions:
An object is a real-world entity.
An object is a runtime entity.
Class Methods
Constructors
Blocks
Nested class and interface
Instance variable in Java
A variable which is created inside the class but outside the method is known as
an instance variable. Instance variable doesn't get memory at compile time. It
gets memory at runtime when an object or instance is created. That is why it is
known as an instance variable.
Method in Java
Inside Class In Java, a method is like a function which is used to expose the behavior of an
object.
Advantage of Method
Code Reusability
Code Optimization
new keyword in Java
The new keyword is used to allocate memory at runtime. All objects get
memory in Heap memory area.
3 Ways to initialize object
There are 3 ways to initialize object in Java.
By reference variable
By method
By constructor
Object StudentRef.java
StudentMeth.java
Initialize StudentCons.java
Before We Started with the Reference variable we should know about the
following facts.
Now, The space in the heap Memory is created but the question is how to
access that space?.
Object Then, We create a Pointing element or simply called Reference variable which
simply points out the Object(the created space in a Heap Memory).
Reference
Understanding Reference variable
Check ConstructorOverloading.java
Constructor
Overloading
This is a keyword used to refer
reference variable to the
current object of the class.
this
Keyword
A method in Java is a block of code that, when called, performs specific actions
mentioned in it.
For instance, if you have written instructions to draw a circle in the method, it
will do that task. You can insert values or parameters into methods, and they
will only be executed when called. They are also referred to as functions.
You can only create a method within a class. There are a total of six components
included in a method declaration. The components provide various information
about the method.
1. Access specifier: It is used to define the access type of the method. Public: You
can access it from any class Private: You can access it within the class where it is
defined Protected: Accessible only in the same package or other subclasses in
Methods in
another package Default: It is the default access specifier used by the Java compiler
if we don’t mention any other specifiers. It is accessible only from the package
where it is declared
Java 2. ReturnType: It defines the return type of the method. In the above syntax, “int”
is the return type. We can mention void as the return type if the method returns
no value.
3. Method name: It is used to give a unique name to the method. In the above
syntax, “addNumbers” is the method name. This tutorial looks at some tips for
naming a method, shortly.
4. Parameter list: It is a list of arguments (data type and variable name) that will be
used in the method.
Method Declaration:
5. Method signature: You don’t have to do anything additional here. The method
signature is just a combination of the method name and parameter list.
6. Method body: This is the set of instructions enclosed within curly brackets that
the method will perform.
Methods in
Java
If a class has multiple methods having same name but different in parameters,
it is known as Method Overloading.
If we have to perform only one operation, having same name of the methods
increases the readability of the program.
Method
Overloading
Type
Promotion
byte can be promoted to short, int, long, float or double. The short datatype
can be promoted to int, long, float or double. The char datatype can be
promoted to int,long,float or double and so on.
Passing
Object to the
method
A method can return an object in a similar manner as that of returning a
variable of primitive types from methods. When a method returns an object,
the return type of the method is the name of the class to which the object
belongs and the normal return statement in the method is used to return the
object.
Returning
Object from
the method
The Java new keyword is used to create an instance of the class. In other words,
it instantiates a class by allocating memory for a new object and returning a
reference to that memory. We can also use the new keyword to create the array
object.
Syntax:
NewExample obj=new NewExample();
new
Points to remember
Operator It is used to create the object.
It allocates the memory at runtime.
All objects occupy memory in the heap area.
It invokes the object constructor.
It requires a single, postfix argument to call the constructor
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.
Keyword
1) Java static variable
If you declare any variable as static, it is known as a static variable. The
static variable can be used to refer to the common property of all
objects (which is not unique for each object), for example, the company
name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the time
of class loading.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an
instance of a class.
static • A static method can access static data member and can change the
value of it.
Keyword Restrictions for the static method
1. The static method can not use non static data member or call non-
static method directly.
2. this and super cannot be used in static context.
static
Keyword
Finalize() is the method of Object class. This method is called just before an
object is garbage collected. finalize() method overrides to dispose system
resources, perform clean-up activities and minimize memory leaks.
Syntax
protected void finalize() throws Throwable
Built-in packages are existing java packages that come along with the JDK. For
example, java.lang, java.util, java.io, etc. For example:
The Java static import was introduced in JDK version 1.5. With the help of static
import, the static variables and methods of imported classes can be accessed.
We don't need to specify the class name or object name to access the methods
or variables. Using a static import statement saves time because the
programmer isn't required to use the class name or object name again and
again.
Example:
static
import static java.util.Math;
import double rt = sqrt(a);
OR
import java.util.Math;
double rt = Math.sqrt(a);
Access control is a mechanism, an attribute of encapsulation which restricts the
access of certain members of a class to specific parts of a program. Access to
members of a class can be controlled using the access modifiers. There are four
access modifiers in Java. They are:
public
protected
default
private
Access
Control
Java Nested class is also known as inner class is a class that is declared inside the
class or interface.
We use inner classes to logically group classes and interfaces in one place to be
more readable and maintainable.
Nested Class
class Java_Inner_class{
//code
}
}
Inner Class non-static nested classes are also known as inner classes.
In simple words, a class that has no name is known as an anonymous inner class in
Java. It should be used if you have to override a method of class or interface.
Anonymous
Inner Class Java Anonymous inner class can be created in two ways:
1. Class (may be abstract or concrete).
2. Interface
Sample 15. Explain object as a method argument and object as a return type of a
method by writing a java program
Questions 16. Explain array as a method argument and array as a return type of a
Unit 3 method by writing a java program
1. Define terms : Array , String, Wrapper class
2. Explain 1D dimensional arrays. Discuss about array declaration, array
initialization and access 1D array elements with suitable example.
3. Explain 2D dimensional arrays. Discuss about array declaration, array
initialization and access 2D array elements with suitable example.
4. What do you mean by jagged array ? Explain with code snippet.
Sample
Questions
Unit 2