SlideShare uma empresa Scribd logo
1 de 16
JDBC –
Java DataBase Connectivity

java.lang
Class Class<T>
public final class Class<T>extends Objectimplements Serializable,
GenericDeclaration, Type, AnnotatedElement
 Instances of the class Class represent classes and interfaces in a
running Java application. An enum is a kind of class and an
annotation is a kind of interface. Every array also belongs to a class
that is reflected as a Class object that is shared by all arrays with
the same element type and number of dimensions. The primitive
Java types (boolean, byte, char, short, int, long, float, and double),
and the keyword void are also represented as Class objects.
 Class has no public constructor. Instead Class objects are
constructed automatically by the Java Virtual Machine as classes
are loaded and by calls to the defineClass method in the class
loader.
2
3
Class.forName() method
 This

method is used to load the class into
JVM’s memory . (without creating the object
also it will loads into the jvm’s memory.
Class.forname(“class”);
Or
Reference=Class.forName(“class”)

4








By using the Class.forName() we can load any class object into JVM’s memory without creating
the object.
Ex; Class c=Class.forName(“java.lang.ArraayList”);
When the class.forName() is executed the code it will create a class boject.
Inside the class object it plays the name of the class which is loaded into JVM and the name of
the package of the class which is loaded into the JVM.
It is a factory method( means capable of constructing its own java class object is called “factory
method”.

5
Ex:
 Class.forName("com.mysql.jdbc.Driver");
 When

this statement is executed, it contains
the static block of given jdbc Driver class, so
the static block executes automatically
 In this static block, there will be a logic to
create JDBC driver class object and to
register that object with driver manager
service through by calling
DriverManager.registerDriver()
6
 Class.forName()

loads the given jdbc Driver
class bcoz of the logic available in the static
block of that jdbc driver class, the jdbc driver
class object will be registered with Driver
Manager service

7
Static
{
mysql.jdbc.Driver jd= new mysql.jdbc.Driver();
Try
{
DriverManager.registerDriver(jd);
}
Catch(SQLException se)
{
se.printStackTrace();
}

8
 public

static Class<?> forName(String
className) throws
ClassNotFoundException

9
 Returns

the Class object associated with the
class or interface with the given string name.
Invoking this method is equivalent to:
Class.forName(className, true,
currentLoader) where currentLoader denotes
the defining class loader of the current
class.For example, the following code
fragment returns the runtime Class descriptor

10
 Class

t = Class.forName("java.lang.Thread")
A call to forName("X") causes the class
named X to be initialized.
 Parameters:className - the fully qualified
name of the desired
class.Returns:the Class object for the class
with the specified
name.Throws:LinkageError - if the linkage
failsExceptionInInitializerError - if the
initialization provoked by this method
failsClassNotFoundException - if the class

11
What is the diff b/w
Class.forName() and Class.forName().newInstance()?
package test;
public class Demo {
public Demo() {
System.out.println("Hi!");
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("test.Demo");
Demo demo = (Demo) clazz.newInstance();
}
}
12
What is the diff b/w
Class.forName() and Class.forName().newInstance()?







calling Class.forName(String) returns the Class object associated
with the class or interface with the given string name i.e. it 
returns test.Demo.class which is affected to the clazz variable of 
type Class.
Then, calling clazz.newInstance() creates a new instance of the
class represented by this Classobject. The class is instantiated as if
by a new expression with an empty argument list. In other words, 
this is here actually equivalent to a new Demo() and returns a new 
instance of Demo.
And running this Demo class thus prints the following output:
Hi!

13
What is the diff b/w
Class.forName() and Class.forName().newInstance()?




The big difference with the traditional new is 
that newInstance allows to instantiate a class that you don't know 
until runtime, making your code more dynamic.
A typical example is the JDBC API which loads, at runtime, the 
exact driver required to perform the work. EJBs containers, Servlet 
containers are other good examples: they use dynamic runtime 
loading to load and create components they don't know anything 
before the runtime.

14
What is the diff b/w
Class.forName() and Class.forName().newInstance()?








(...) A Driver class is loaded, and therefore automatically registered 
with the DriverManager, in one of two ways:
by calling the method Class.forName. This explicitly loads the driver 
class. Since it does not depend on any external setup, this way of 
loading a driver is the recommended one for using 
theDriverManager framework. The following code loads the 
class acme.db.Driver:
Class.forName("acme.db.Driver");If acme.db.Driver has been
written so that loading it causes an instance to be created and
also calls DriverManager.registerDriver with that instance as
the parameter (as it should do), then it is in the DriverManager's list 
of drivers and available for creating a connection.
(...)
15
What is the diff b/w
Class.forName() and Class.forName().newInstance()?


In both of these cases, it is the responsibility of the newlyloaded Driver class to register itself by 
calling DriverManager.registerDriver. As mentioned, this should be 
done automatically when the class is loaded.

16

Mais conteúdo relacionado

Mais procurados

ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Sagar Verma
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVATech_MX
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objectsDeepak Singh
 

Mais procurados (18)

ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
 
Java Tutorial 1
Java Tutorial 1Java Tutorial 1
Java Tutorial 1
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Chapter 7 java
Chapter 7 javaChapter 7 java
Chapter 7 java
 
Java Programming - 05 access control in java
Java Programming - 05 access control in javaJava Programming - 05 access control in java
Java Programming - 05 access control in java
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
 

Destaque

Destaque (8)

Interface connection
Interface connectionInterface connection
Interface connection
 
Different waysconnect
Different waysconnectDifferent waysconnect
Different waysconnect
 
Get data
Get dataGet data
Get data
 
Exceptions
ExceptionsExceptions
Exceptions
 
Class
ClassClass
Class
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
2. attributes
2. attributes2. attributes
2. attributes
 

Semelhante a Class

Java class loading tips and tricks - Java Colombo Meetup, January, 2014
Java class loading  tips and tricks - Java Colombo Meetup, January, 2014Java class loading  tips and tricks - Java Colombo Meetup, January, 2014
Java class loading tips and tricks - Java Colombo Meetup, January, 2014Sameera Jayasoma
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers GuideDaisyWatson5
 
Class loader basic
Class loader basicClass loader basic
Class loader basic명철 강
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAVINASH KUMAR
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityAtul Saurabh
 
Do you really get class loaders?
Do you really get class loaders? Do you really get class loaders?
Do you really get class loaders? guestd56374
 
Reflection in java
Reflection in javaReflection in java
Reflection in javaupen.rockin
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.pptDrMeenakshiS
 
A Scala tutorial
A Scala tutorialA Scala tutorial
A Scala tutorialDima Statz
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 

Semelhante a Class (20)

Java class loading tips and tricks - Java Colombo Meetup, January, 2014
Java class loading  tips and tricks - Java Colombo Meetup, January, 2014Java class loading  tips and tricks - Java Colombo Meetup, January, 2014
Java class loading tips and tricks - Java Colombo Meetup, January, 2014
 
Java Reflection Concept and Working
Java Reflection Concept and WorkingJava Reflection Concept and Working
Java Reflection Concept and Working
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers Guide
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Java Class Loading
Java Class LoadingJava Class Loading
Java Class Loading
 
Diving into Java Class Loader
Diving into Java Class LoaderDiving into Java Class Loader
Diving into Java Class Loader
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Do you really get class loaders?
Do you really get class loaders? Do you really get class loaders?
Do you really get class loaders?
 
Reflection in java
Reflection in javaReflection in java
Reflection in java
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
A Scala tutorial
A Scala tutorialA Scala tutorial
A Scala tutorial
 
Java14
Java14Java14
Java14
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
JVM
JVMJVM
JVM
 

Mais de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 
Properties
PropertiesProperties
Properties
 
Java.sql package
Java.sql packageJava.sql package
Java.sql package
 
Interface callable statement
Interface callable statementInterface callable statement
Interface callable statement
 

Último

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Último (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 

Class

  • 1. JDBC – Java DataBase Connectivity java.lang Class Class<T>
  • 2. public final class Class<T>extends Objectimplements Serializable, GenericDeclaration, Type, AnnotatedElement  Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.  Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader. 2
  • 3. 3
  • 4. Class.forName() method  This method is used to load the class into JVM’s memory . (without creating the object also it will loads into the jvm’s memory. Class.forname(“class”); Or Reference=Class.forName(“class”) 4
  • 5.      By using the Class.forName() we can load any class object into JVM’s memory without creating the object. Ex; Class c=Class.forName(“java.lang.ArraayList”); When the class.forName() is executed the code it will create a class boject. Inside the class object it plays the name of the class which is loaded into JVM and the name of the package of the class which is loaded into the JVM. It is a factory method( means capable of constructing its own java class object is called “factory method”. 5
  • 6. Ex:  Class.forName("com.mysql.jdbc.Driver");  When this statement is executed, it contains the static block of given jdbc Driver class, so the static block executes automatically  In this static block, there will be a logic to create JDBC driver class object and to register that object with driver manager service through by calling DriverManager.registerDriver() 6
  • 7.  Class.forName() loads the given jdbc Driver class bcoz of the logic available in the static block of that jdbc driver class, the jdbc driver class object will be registered with Driver Manager service 7
  • 8. Static { mysql.jdbc.Driver jd= new mysql.jdbc.Driver(); Try { DriverManager.registerDriver(jd); } Catch(SQLException se) { se.printStackTrace(); } 8
  • 9.  public static Class<?> forName(String className) throws ClassNotFoundException 9
  • 10.  Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to: Class.forName(className, true, currentLoader) where currentLoader denotes the defining class loader of the current class.For example, the following code fragment returns the runtime Class descriptor 10
  • 11.  Class t = Class.forName("java.lang.Thread") A call to forName("X") causes the class named X to be initialized.  Parameters:className - the fully qualified name of the desired class.Returns:the Class object for the class with the specified name.Throws:LinkageError - if the linkage failsExceptionInInitializerError - if the initialization provoked by this method failsClassNotFoundException - if the class 11
  • 12. What is the diff b/w Class.forName() and Class.forName().newInstance()? package test; public class Demo { public Demo() { System.out.println("Hi!"); } @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { Class clazz = Class.forName("test.Demo"); Demo demo = (Demo) clazz.newInstance(); } } 12
  • 13. What is the diff b/w Class.forName() and Class.forName().newInstance()?     calling Class.forName(String) returns the Class object associated with the class or interface with the given string name i.e. it  returns test.Demo.class which is affected to the clazz variable of  type Class. Then, calling clazz.newInstance() creates a new instance of the class represented by this Classobject. The class is instantiated as if by a new expression with an empty argument list. In other words,  this is here actually equivalent to a new Demo() and returns a new  instance of Demo. And running this Demo class thus prints the following output: Hi! 13
  • 14. What is the diff b/w Class.forName() and Class.forName().newInstance()?   The big difference with the traditional new is  that newInstance allows to instantiate a class that you don't know  until runtime, making your code more dynamic. A typical example is the JDBC API which loads, at runtime, the  exact driver required to perform the work. EJBs containers, Servlet  containers are other good examples: they use dynamic runtime  loading to load and create components they don't know anything  before the runtime. 14
  • 15. What is the diff b/w Class.forName() and Class.forName().newInstance()?     (...) A Driver class is loaded, and therefore automatically registered  with the DriverManager, in one of two ways: by calling the method Class.forName. This explicitly loads the driver  class. Since it does not depend on any external setup, this way of  loading a driver is the recommended one for using  theDriverManager framework. The following code loads the  class acme.db.Driver: Class.forName("acme.db.Driver");If acme.db.Driver has been written so that loading it causes an instance to be created and also calls DriverManager.registerDriver with that instance as the parameter (as it should do), then it is in the DriverManager's list  of drivers and available for creating a connection. (...) 15
  • 16. What is the diff b/w Class.forName() and Class.forName().newInstance()?  In both of these cases, it is the responsibility of the newlyloaded Driver class to register itself by  calling DriverManager.registerDriver. As mentioned, this should be  done automatically when the class is loaded. 16