[go: up one dir, main page]

0% found this document useful (0 votes)
5 views10 pages

Java Methods and Java Class

The document provides an overview of key concepts in Java, including the importance of the main method, differences between static and instance methods, and the concept of immutable objects. It explains how to create immutable classes, the definition of singleton classes, and the purpose of adapter classes. Additionally, it discusses inner and nested classes, anonymous inner classes, and the Object class as the root class for all Java classes.

Uploaded by

rdipali162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Java Methods and Java Class

The document provides an overview of key concepts in Java, including the importance of the main method, differences between static and instance methods, and the concept of immutable objects. It explains how to create immutable classes, the definition of singleton classes, and the purpose of adapter classes. Additionally, it discusses inner and nested classes, anonymous inner classes, and the Object class as the root class for all Java classes.

Uploaded by

rdipali162
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Methods

1. What is the importance of main method in Java?


The main() method in Java is the starting point for
JVM to start execution of a Java program1. It is the
first method that encounters first during execution and
is the entry point of a program2. The main() method is
static so that JVM can invoke it without instantiating the
class, which saves memory3. Without the main()
method, JVM will not execute the program

2. What is difference between static (class) method and instance


method?
The difference between static (class)
method and instance method is12:
 Instance methods require an object of its class to
be created before it can be called. Static
methods can be called without creating an object
of class.

 Instance methods can modify the behavior of the


instance variables. Class methods can modify the
behavior of the class, that reflects to the entire
class so with the all instances of the class.

 Static methods perform their tasks in isolation.


They didn't have any interaction with the class or
instance methods.

3. Can you have virtual functions in Java?


4. How to call main() method in java?

5. Can we execute a program without main () method?


Yes, we can execute a java program without a main
method by using a static block.
Static block in Java is a group of statements that
gets executed only once when the class is loaded
into the memory by Java ClassLoader, It is also
known as a static initialization block. Static
initialization block is going directly into the stack
memory.
6. Example
7. class StaticInitializationBlock{
8. static{
9. System.out.println("class without a main
method");
10. System.exit(0);
11. }
12. }

6. What is variable argument method in Java?


Variable Arguments (Varargs) in Java is a method that
takes a variable number of arguments. Variable
Arguments in Java simplifies the creation of methods
that need to take a variable number of arguments.
For Example,
public static void fun(int ... a)
{
// method body
}

7. What are function call by pass by reference and pass by value?


Call by Reference
In call by reference method of parameter passing, the
address of the actual parameters is passed to the
function as the formal parameters.
 Both the actual and formal parameters refer to
the same locations.
 Any changes made inside the function are
actually reflected in the actual parameters of
the caller.

Java class
1. Differentiate between class and object with practical
example.

Class Object

Class is the blueprint of An object is an instance


Class Object

an object. It is used to
of the class.
create objects.

No memory is allocated Memory is allocated as


when a class is soon as an object is
declared. created.

An object is a real-world
A class is a group of
entity such as a book,
similar objects.
car, etc.

An object is a physical
Class is a logical entity.
entity.

Objects can be created


A class can only be
many times as per
declared once.
requirement.

Objects of the class car


An example of class
can be BMW, Mercedes,
can be a car.
Ferrari, etc.

2. What is Immutable Object? Can you write Immutable


Class?

The immutable objects are objects whose value can not


be changed after initialization. We can not change
anything once the object is created. For
example, primitive objects such
as int, long, float, double, all legacy
classes, Wrapper class, String class, etc.

In a nutshell, immutable means unmodified or


unchangeable. Once the immutable objects are
created, its object values and state can not be changed

How to Create an Immutable Class

The following things are essential for creating an


immutable class:

o Final class, which is declared as final so that it can't


be extended.
o All fields should be private so that direct access to
the fields is blocked.
o No Setters
o All mutable fields should be as final so that they
can not be iterated once initialized.

Consider the below example:

1. public class JtpExample1 {


2. private final String s;
3. JtpExample1(final String s) {
4. this.s = s;
5. }
6. public final String getName() {
7. return s;
8. }
9. public static void main(String[] args) {
10. JtpExample obj = new JtpExample("Core J
ava Training");
11. System.out.println(obj.getName());
12. }
13. }

Output:
Core Java Training

3. What is singleton class? When it is used?


In object-oriented programming, a java singleton class
is a class that can have only one object (an instance of
the class) at a time. After the first time, if we try to
instantiate the Java Singleton classes, the new variable
also points to the first instance created.

4. What is adapter class? What is its purpose?


Adapter Class is a simple java class that
implements an interface with only an empty
implementation.
5. What is inner and nested class? How to access members
of these classes?
In Java, inner class refers to the class that is declared
inside class or interface which were mainly introduced,
to sum up, same logically relatable classes as Java is
purely object-oriented so bringing it closer to the real
world. Now geeks you must be wondering why they
were introduced?
There are certain advantages associated with
inner classes are as follows:
 Making code clean and readable.
 Private methods of the outer class can be
accessed, so bringing a new dimension and
making it closer to the real world.
 Optimizing the code module.

Type 1: Nested Inner Class


It can access any private instance variable of the outer
class. Like any other instance variable, we can have
access modifier private, protected, public, and default
modifier. Like class, an interface can also be nested
and can have access specifiers.

6. What is anonymous inner class?

Java anonymous inner class is an inner class without a


name and for which only a single object is created. An
anonymous inner class can be useful when making an
instance of an object with certain "extras" such as
overloading methods of a class or interface, without
having to actually subclass a class.

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.
Java Anonymous inner class can be created in two
ways:

8. How would you prevent a client from directly


instantiating your concrete classes?
7. What is the root class for all java classes?

The Object class of the java.lang package is the root


class in Java i.e. It is the super class of every user-
defined/predefined class n Java. All objects, including
arrays, implement the methods of this class.
The reason for this is to have common functionalities
such as synchronization, garbage collection, collection
support, object cloning for all objects in Java.
The class named Class of java.lang package provides a
method named getSuperclass() this method returns the
Class representing the superclass of the current Class.

You might also like