SlideShare a Scribd company logo
1 of 3
Download to read offline
91-9582167401																						EDUFECT	
JT-M-CLASA	
www.edufect.com	 PERFECT JAVA	
	
	 -	1	-	
Defining	a	class	
Classes	are	defined	with	class	keyword	followed	by	<class-name>.	Class	names	should	start	with	
capital	letter.	
	
Syntax:	
1 class <class_name> //you can use any name for your class except keywords
2 {
3 //body of class
4 //define variables here that will be shared between objects of this class
5 }
	
Example	1:	A	simple	class	named	Student		
6 class Student
7 {
8 String name;
9 int rollNo;
10 }
Creating	objects	
In	java	new	keyword	is	used	to	create	an	object.	
	
Syntax:		
<class_name> object_name = new <class_name>();
	
Example	2:	Creating	object	of	our	Student	class		
Student st = new Student();
Accessing	variables	defined	inside	a	class	
OR	
What	is	.(dot)	operator	in	java?	And	how	to	use	it?	
.(dot)	is	an	operator.		It	is	used	to	access	variables	defined	in	a	class	through	its	object.	We	cannot	
directly	access	these	variables.	
	
Syntax:		
object-name.variable = value;
	
Example	3:	Create	an	object	of	Student	class	and	give	values	to	variables	defined	in	this	class.	
1 Student obj = new Student();
2 obj.name = "Rohit";
3 obj.rollNo = 5;
4 System.out.println(obj.name);
5 System.out.println(obj.rollNo);
Output:	
Rohit
5
Explanation:	In	this	example	we	are	accessing	the	variable	"name"	and	"rollNo"	with	dot	operator	
and	assigning	values	to	them.	Now	name	is	Rohit	and	rollNo	is	5.
91-9582167401																						EDUFECT	
JT-M-CLASA	
www.edufect.com	 PERFECT JAVA	
	
	 -	2	-	
Default	values	of	variables	in	class	
After	object	creation	every	variables	gets	initialized	with	default	value	based	on	its	data	type.	
Data	type	 Default	value	
boolean	 false	
char	 'u0000'	
int	 0	
double	 0.0	
Any	reference	type	(eg.	String)	 null	
	
Example	4:	Default	Values
1 Student obj = new Student();
2
3 System.out.println(obj.name); //before any values are assigned
4 System.out.println(obj.rollNo);
5
6 obj.name = "Vivek"; //after giving values
7 obj.rollNo = 50;
8
9 System.out.println(obj.name);
10 System.out.println(obj.rollNo);
Output:	
null
0
Vivek
50
Creating	multiple	objects	of	a	class
Just	like	variables	you	can	create	multiple	objects.	
	
Example	5:	Multiple	Objects
Class:	
1 class StudentWithMarks
2 {
3 String name;
4 int marks;
5 int rollNo;
6 }
Code:	
1 StudentWithMarks st1, st2;
2 st1 = new StudentWithMarks();
3 st2 = new StudentWithMarks();
4
5 st1.name = "Sanchit";
6 st1.rollNo = 10;
7 st1.marks = 90;
8
9 st2.name = "Mohit";
91-9582167401																						EDUFECT	
JT-M-CLASA	
www.edufect.com	 PERFECT JAVA	
	
	 -	3	-	
10
11 System.out.println(st1.name);
12 System.out.println(st1.rollNo);
13 System.out.println(st1.marks);
14
15 System.out.println(st2.name);
16 System.out.println(st2.rollNo); //default value
17 System.out.println(st2.marks); //default value
Output:	
Sanchit
10
90
Mohit
0
0
	
Example	6:	Create	a	class	Point	that	contains	two	fields	x	and	y	of	type	int.
	
1 class Point
2 {
3 int x;
4 int y;
5 }
	
Example	7:	Create	two	objects	of	class	Point.	Give	them	random	values	and	print	these	values	
	
1 Point p1 = new Point();
2 p1.x = 4;
3 p1.y = 1;
4
5 Point p2 = new Point();
6 p2.x = 6;
7 p2.y = 5;
8
9 System.out.println("Point 1 ");
10 System.out.println("x = "+p1.x);
11 System.out.println("y = "+p1.y);
12 System.out.println("Point 2 ");
13 System.out.println("x = "+p2.x);
14 System.out.println("y = "+p2.y);
Output:	
Point 1
x = 4
y = 1
Point 2
x = 6
y = 5

More Related Content

What's hot (19)

Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Oops in java
Oops in javaOops in java
Oops in java
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 

Similar to Defining Classes and Objects in Java

packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Class properties
Class propertiesClass properties
Class propertiesSiva Priya
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods javaPadma Kannan
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
My Object Oriented.pptx
My Object Oriented.pptxMy Object Oriented.pptx
My Object Oriented.pptxGopalNarayan7
 
The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31Mahmoud Samir Fayed
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptRithwikRanjan
 
OOP using java (Variable in java)
OOP using java (Variable in java)OOP using java (Variable in java)
OOP using java (Variable in java)omeed
 
Object Oriented Programming in Android Studio
Object Oriented Programming in Android StudioObject Oriented Programming in Android Studio
Object Oriented Programming in Android StudioMahmoodGhaemMaghami
 

Similar to Defining Classes and Objects in Java (20)

packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Class properties
Class propertiesClass properties
Class properties
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Chapter 7 java
Chapter 7 javaChapter 7 java
Chapter 7 java
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
My Object Oriented.pptx
My Object Oriented.pptxMy Object Oriented.pptx
My Object Oriented.pptx
 
Computer programming 2 Lesson 3
Computer programming 2  Lesson 3Computer programming 2  Lesson 3
Computer programming 2 Lesson 3
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
python.pptx
python.pptxpython.pptx
python.pptx
 
The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31The Ring programming language version 1.4.1 book - Part 20 of 31
The Ring programming language version 1.4.1 book - Part 20 of 31
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
OOP using java (Variable in java)
OOP using java (Variable in java)OOP using java (Variable in java)
OOP using java (Variable in java)
 
Object Oriented Programming in Android Studio
Object Oriented Programming in Android StudioObject Oriented Programming in Android Studio
Object Oriented Programming in Android Studio
 
Reflection
ReflectionReflection
Reflection
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Defining Classes and Objects in Java

  • 1. 91-9582167401 EDUFECT JT-M-CLASA www.edufect.com PERFECT JAVA - 1 - Defining a class Classes are defined with class keyword followed by <class-name>. Class names should start with capital letter. Syntax: 1 class <class_name> //you can use any name for your class except keywords 2 { 3 //body of class 4 //define variables here that will be shared between objects of this class 5 } Example 1: A simple class named Student 6 class Student 7 { 8 String name; 9 int rollNo; 10 } Creating objects In java new keyword is used to create an object. Syntax: <class_name> object_name = new <class_name>(); Example 2: Creating object of our Student class Student st = new Student(); Accessing variables defined inside a class OR What is .(dot) operator in java? And how to use it? .(dot) is an operator. It is used to access variables defined in a class through its object. We cannot directly access these variables. Syntax: object-name.variable = value; Example 3: Create an object of Student class and give values to variables defined in this class. 1 Student obj = new Student(); 2 obj.name = "Rohit"; 3 obj.rollNo = 5; 4 System.out.println(obj.name); 5 System.out.println(obj.rollNo); Output: Rohit 5 Explanation: In this example we are accessing the variable "name" and "rollNo" with dot operator and assigning values to them. Now name is Rohit and rollNo is 5.
  • 2. 91-9582167401 EDUFECT JT-M-CLASA www.edufect.com PERFECT JAVA - 2 - Default values of variables in class After object creation every variables gets initialized with default value based on its data type. Data type Default value boolean false char 'u0000' int 0 double 0.0 Any reference type (eg. String) null Example 4: Default Values 1 Student obj = new Student(); 2 3 System.out.println(obj.name); //before any values are assigned 4 System.out.println(obj.rollNo); 5 6 obj.name = "Vivek"; //after giving values 7 obj.rollNo = 50; 8 9 System.out.println(obj.name); 10 System.out.println(obj.rollNo); Output: null 0 Vivek 50 Creating multiple objects of a class Just like variables you can create multiple objects. Example 5: Multiple Objects Class: 1 class StudentWithMarks 2 { 3 String name; 4 int marks; 5 int rollNo; 6 } Code: 1 StudentWithMarks st1, st2; 2 st1 = new StudentWithMarks(); 3 st2 = new StudentWithMarks(); 4 5 st1.name = "Sanchit"; 6 st1.rollNo = 10; 7 st1.marks = 90; 8 9 st2.name = "Mohit";
  • 3. 91-9582167401 EDUFECT JT-M-CLASA www.edufect.com PERFECT JAVA - 3 - 10 11 System.out.println(st1.name); 12 System.out.println(st1.rollNo); 13 System.out.println(st1.marks); 14 15 System.out.println(st2.name); 16 System.out.println(st2.rollNo); //default value 17 System.out.println(st2.marks); //default value Output: Sanchit 10 90 Mohit 0 0 Example 6: Create a class Point that contains two fields x and y of type int. 1 class Point 2 { 3 int x; 4 int y; 5 } Example 7: Create two objects of class Point. Give them random values and print these values 1 Point p1 = new Point(); 2 p1.x = 4; 3 p1.y = 1; 4 5 Point p2 = new Point(); 6 p2.x = 6; 7 p2.y = 5; 8 9 System.out.println("Point 1 "); 10 System.out.println("x = "+p1.x); 11 System.out.println("y = "+p1.y); 12 System.out.println("Point 2 "); 13 System.out.println("x = "+p2.x); 14 System.out.println("y = "+p2.y); Output: Point 1 x = 4 y = 1 Point 2 x = 6 y = 5