SlideShare uma empresa Scribd logo
1 de 14
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);
}
}

Mais conteúdo relacionado

Mais procurados (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
class and objects
class and objectsclass and objects
class and objects
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 

Semelhante a Class introduction in java

UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
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 Javakjkleindorfer
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
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
 
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,UoMambikavenkatesh2
 
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 JavaRadhika Talaviya
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programmingshinyduela
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.pptKhizar40
 
Java tutorials
Java tutorialsJava tutorials
Java tutorialssaryu2011
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 

Semelhante a 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
 
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
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 

Mais de yugandhar vadlamudi (15)

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
 
Operators in java
Operators in javaOperators in java
Operators in java
 
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
 

Último

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 

Último (20)

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

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); } }