SlideShare a Scribd company logo
Class Introduction
Class
• Java class is nothing but a template for object you are going to create or it’s a blue
print by using this we create an object.
Class (name of the class)
{
(Here define member of class)
}
• A class is a user defined datatype with a template that serves to define its properties
• Members of a Class
• Field: instance variables are called fields
• Method: method is nothing but the operation that an object can
perform
Class Stock
{
/*fields */
Public commodity;
Public price;
/*method /
ublic void buy (int no_of_commodity) {}
ublic boolean sale () {}
}
Variables and method declaration
• Data is encapsulated in class by placing variables inside the body of the
class
class Demo{
int int_variable,a;
}
• A method is a set of code which is referred to by name and can be called
(invoked) at any point in a program
• Method declaration has four basic parts
• Method name,Return type,Argument list,Method body
class Demo
{
type method-name(parameter list)
{
method body;
}
}
class Demo_class
{
/*
instance variable
instance method
*/
int int_variable=1;
public void demo_()
{
System.out.println("hello");
}
}
Object
• "object" refers to a particular instance of a class where the object can be
a combination of variables, functions, and data structures and block of
Memory to store all variables,fuctions,data structure.
• Creating an Object :Objects in java are created using the new operator.
The new operator creates the objects of the specified class and return the
referenc to that object
class_name <object_name>=new class_name();
class Demo_class
{
int int_variable;
public void demo()
{
System.out.println("hello");
}
public static void main(String[] y)
{
/*
*creating and object
*/
Demo_class a=new Demo_class();
System.out.println("what contains in a object "+a);
}
}
Accessing instance variables and
methods
class Demo_class
{
int int_variable=10;
public void demo()
{
System.out.println("hello");
}
public static void main(String[] y)
{
/*
*creating and object
*/
Demo_class a=new Demo_class();
a.demo();
System.out.println("the value of int_variable is"+a.int_variable);
System.out.println("what contains in a object "+a);
}
}
Assigning Object reference variables
• Object reference is the variable that holds the memory location of the real
object. Object Reference variable is just like pointer in c but not exactly
class_name <object_name>=new class_name();
class_name <refference_name>=object_name;
class Demo_class
{
int int_variable=10;
public void demo()
{
System.out.println("hello");
}
public static void main(String[] y)
{
/*
*creating and object
*/
Demo_class a=new Demo_class();
Demo_class b=a;
b.int_variable=20;
System.out.println("the value of int_variable is"+a.int_variable);
}
}
Methods
• A method is a group of Java statements that perform some operation on
some data, and may or may not return a result.
class Demo_class
{ public void demo()
{System.out.println("hello");}
/* method with return type */
public int demo_1()
{return (2);}
/*method with arguments*/
public void demo_2(int a)
{ System.out.println("the parameter a value is::"+a);}
public static void main(String[] y)
{/* *creating and object */
Demo_class a=new Demo_class();
/*method calling */
a.demo();
System.out.println("the return type value is "+a.demo_1());
a.demo_2(10);
}
}
Constructor
• When ever we are creating an object some piece of code will execute
internally to perform initialization, that piece of code is nothing but
“constructor“.
Rules for writing constructors
• The name of the class and name of the constructor must be same
• A constructor may have or may not have parameters
• If a constructor doesn’t have any parameters, it is called ‘default‘ constructor.
• If a constructor has 1 or more parameters, It is called “Parameterized constructor.
• Return type is not allowed for the constructors even void also
• the only applicable modifiers for the constructors are private, protected, default, public. If we
are using any other modifiers we will get compile time error.
• A constructor is automatically called and executed at the time of creating an object.
• If nothing is passed to the object,then default constructor is called and executed.if some
values are passed to the object ,then the parameterized constructor is called
• A constructor is called and executed only once per object
•
class Demo_class
{
Demo_class()
{ System.out.println("helloworld"); }
Demo_class(String s)
{ System.out.println("The string value::"+s); }
void Demo_class()
{System.out.println("hell");}
public static void main(String[] y)
{Demo_class d=new Demo_class();
Demo_class d1=new Demo_class("yugandhar");
Demo_class d2=new Demo_class();
d2.Demo_class();
}//public
}//class
This Keyword
• Which can be used inside method or constructor of class
• It(this) works as a reference to current object whose method
or constructor is being invoked
• this keyword can be used to refer any member of current object from
within an instance method or a constructor.
Usage of java this keyword
• this keyword can be used to refer current class instance variable.
• this() can be used to invoke current class constructor.
• this keyword can be used to invoke current class method (implicitly)
• this can be passed as an argument in the method call.
• this can be passed as argument in the constructor call.
• this keyword can also be used to return the current class instance.
Instance variable hiding with this key
word
class Demo_class
{
int i,j=20;
public void demo(int i)
{
this.i=i;
System.out.println("the value i in method"+i);
}
public static void main(String[] y)
{
Demo_class d=new Demo_class();
d.demo(30);
System.out.println("the class variable i::"+d.i);
}
}
Garbage Collection
• Java destruction of object from memory is done automatically by the JVM.
When there is no reference to an object, then that object is assumed to be
no longer needed and the memory occupied by the object are released.
This technique is called Garbage Collection. This is accomplished by the
JVM.
Advantage of Garbage Collection
• It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
• It is automatically done by the garbage collector(a part of JVM) so we
don't need to make extra efforts.
• Correct the error in the following code
Class Variable_Name
{
Public static void main(string[] y)
{
Sytem.out.println(“hello world”);
}
};
Correct the Error in the Following Code
Class Return_value
{
public string name=“team”;
public string getName()
{
System.out.println(“hello world”);
return(“hello”);
}
public static void main(String[] y)
{
Return_value rv=new Return_Value();
String y;
y=rv.getName();
System.out.println(“”+y);
}
}

More Related Content

What's hot

String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
Lovely Professional University
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Java Collections
Java  Collections Java  Collections
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
atozknowledge .com
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Operators in java
Operators in javaOperators in java
Operators in java
yugandhar vadlamudi
 

What's hot (20)

String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Java operators
Java operatorsJava operators
Java operators
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Similar to Class introduction in java

UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
akila m
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
SarthakSrivastava70
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
Shalabh Chaudhary
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
kjkleindorfer
 
C#2
C#2C#2
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
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
Sagar Verma
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
ambikavenkatesh2
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
Sonu WIZIQ
 
Oops
OopsOops
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
Khizar40
 
Java tutorials
Java tutorialsJava tutorials
Java tutorialssaryu2011
 

Similar to Class introduction in java (20)

UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Constructor
ConstructorConstructor
Constructor
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
C#2
C#2C#2
C#2
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
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
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Oops
OopsOops
Oops
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 

More from yugandhar vadlamudi

Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
yugandhar vadlamudi
 
Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
yugandhar vadlamudi
 
Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
yugandhar vadlamudi
 
JButton in Java Swing example
JButton in Java Swing example JButton in Java Swing example
JButton in Java Swing example
yugandhar vadlamudi
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
yugandhar vadlamudi
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
yugandhar vadlamudi
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
yugandhar vadlamudi
 
JMenu Creation in Java Swing
JMenu Creation in Java SwingJMenu Creation in Java Swing
JMenu Creation in Java Swing
yugandhar vadlamudi
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
yugandhar vadlamudi
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
 
Inheritance
InheritanceInheritance
Inheritance
yugandhar vadlamudi
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
yugandhar vadlamudi
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
yugandhar vadlamudi
 

More from yugandhar vadlamudi (14)

Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
 
Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
 
Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
 
JButton in Java Swing example
JButton in Java Swing example JButton in Java Swing example
JButton in Java Swing example
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
JMenu Creation in Java Swing
JMenu Creation in Java SwingJMenu Creation in Java Swing
JMenu Creation in Java Swing
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Inheritance
InheritanceInheritance
Inheritance
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
 

Recently uploaded

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 

Recently uploaded (20)

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 

Class introduction in java

  • 2. Class • Java class is nothing but a template for object you are going to create or it’s a blue print by using this we create an object. Class (name of the class) { (Here define member of class) } • A class is a user defined datatype with a template that serves to define its properties • Members of a Class • Field: instance variables are called fields • Method: method is nothing but the operation that an object can perform Class Stock { /*fields */ Public commodity; Public price; /*method / ublic void buy (int no_of_commodity) {} ublic boolean sale () {} }
  • 3. Variables and method declaration • Data is encapsulated in class by placing variables inside the body of the class class Demo{ int int_variable,a; } • A method is a set of code which is referred to by name and can be called (invoked) at any point in a program • Method declaration has four basic parts • Method name,Return type,Argument list,Method body class Demo { type method-name(parameter list) { method body; } }
  • 4. class Demo_class { /* instance variable instance method */ int int_variable=1; public void demo_() { System.out.println("hello"); } }
  • 5. Object • "object" refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures and block of Memory to store all variables,fuctions,data structure. • Creating an Object :Objects in java are created using the new operator. The new operator creates the objects of the specified class and return the referenc to that object class_name <object_name>=new class_name(); class Demo_class { int int_variable; public void demo() { System.out.println("hello"); } public static void main(String[] y) { /* *creating and object */ Demo_class a=new Demo_class(); System.out.println("what contains in a object "+a); } }
  • 6. Accessing instance variables and methods class Demo_class { int int_variable=10; public void demo() { System.out.println("hello"); } public static void main(String[] y) { /* *creating and object */ Demo_class a=new Demo_class(); a.demo(); System.out.println("the value of int_variable is"+a.int_variable); System.out.println("what contains in a object "+a); } }
  • 7. Assigning Object reference variables • Object reference is the variable that holds the memory location of the real object. Object Reference variable is just like pointer in c but not exactly class_name <object_name>=new class_name(); class_name <refference_name>=object_name; class Demo_class { int int_variable=10; public void demo() { System.out.println("hello"); } public static void main(String[] y) { /* *creating and object */ Demo_class a=new Demo_class(); Demo_class b=a; b.int_variable=20; System.out.println("the value of int_variable is"+a.int_variable); } }
  • 8. Methods • A method is a group of Java statements that perform some operation on some data, and may or may not return a result. class Demo_class { public void demo() {System.out.println("hello");} /* method with return type */ public int demo_1() {return (2);} /*method with arguments*/ public void demo_2(int a) { System.out.println("the parameter a value is::"+a);} public static void main(String[] y) {/* *creating and object */ Demo_class a=new Demo_class(); /*method calling */ a.demo(); System.out.println("the return type value is "+a.demo_1()); a.demo_2(10); } }
  • 9. Constructor • When ever we are creating an object some piece of code will execute internally to perform initialization, that piece of code is nothing but “constructor“. Rules for writing constructors • The name of the class and name of the constructor must be same • A constructor may have or may not have parameters • If a constructor doesn’t have any parameters, it is called ‘default‘ constructor. • If a constructor has 1 or more parameters, It is called “Parameterized constructor. • Return type is not allowed for the constructors even void also • the only applicable modifiers for the constructors are private, protected, default, public. If we are using any other modifiers we will get compile time error. • A constructor is automatically called and executed at the time of creating an object. • If nothing is passed to the object,then default constructor is called and executed.if some values are passed to the object ,then the parameterized constructor is called • A constructor is called and executed only once per object •
  • 10. class Demo_class { Demo_class() { System.out.println("helloworld"); } Demo_class(String s) { System.out.println("The string value::"+s); } void Demo_class() {System.out.println("hell");} public static void main(String[] y) {Demo_class d=new Demo_class(); Demo_class d1=new Demo_class("yugandhar"); Demo_class d2=new Demo_class(); d2.Demo_class(); }//public }//class
  • 11. This Keyword • Which can be used inside method or constructor of class • It(this) works as a reference to current object whose method or constructor is being invoked • this keyword can be used to refer any member of current object from within an instance method or a constructor. Usage of java this keyword • this keyword can be used to refer current class instance variable. • this() can be used to invoke current class constructor. • this keyword can be used to invoke current class method (implicitly) • this can be passed as an argument in the method call. • this can be passed as argument in the constructor call. • this keyword can also be used to return the current class instance.
  • 12. Instance variable hiding with this key word class Demo_class { int i,j=20; public void demo(int i) { this.i=i; System.out.println("the value i in method"+i); } public static void main(String[] y) { Demo_class d=new Demo_class(); d.demo(30); System.out.println("the class variable i::"+d.i); } }
  • 13. Garbage Collection • Java destruction of object from memory is done automatically by the JVM. When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object are released. This technique is called Garbage Collection. This is accomplished by the JVM. Advantage of Garbage Collection • It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory. • It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
  • 14. • Correct the error in the following code Class Variable_Name { Public static void main(string[] y) { Sytem.out.println(“hello world”); } }; Correct the Error in the Following Code Class Return_value { public string name=“team”; public string getName() { System.out.println(“hello world”); return(“hello”); } public static void main(String[] y) { Return_value rv=new Return_Value(); String y; y=rv.getName(); System.out.println(“”+y); } }