SlideShare a Scribd company logo
2
Most read
7
Most read
8
Most read
Java Class Structure
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam 1
2
Classes
 A class describes a set of objects
 The objects are called instances of the class
 A class describes:
 Fields (instance variables)that hold the data for each object
 Constructors that tell how to create a new object of this class
 Methods that describe the actions the object can perform
 In addition, a class can have data and methods of its own
(not part of the objects)
 For example, it can keep a count of the number of objects it has
created
 Such data and methods are called static
 We are avoiding static data and methods for the time being
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
3
Defining a class
 Here is the simplest syntax for defining a class:
class ClassName {
// the fields (variables) of the object
// the constructors for the object
// the methods of the object
}
 You can put public, protected, or private before the word
class
 Things in a class can be in any order (I recommend the
above order)
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
4
Defining fields
 An object’s data is stored in fields (also called
instance variables)
 The fields describe the state of the object
 Fields are defined with ordinary variable declarations:
 String name;
Double health;
int age = 0;
 Instance variables are available throughout the entire
class that declares them
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Defining a method
 A method has the syntax:
return-type method-name(parameters) {
method-variables
code
}
 Example:
boolean isAdult(int age) {
int magicAge = 21;
return age >= magicAge;
}
 Example:
double average(int a, int b) {
return (a + b) / 2.0;
}
5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
6
Methods may have local variables
 A method may have local (method) variables
 Formal parameters are a kind of local variable
 int add(int m, int n) {
int sum = m + n;
return sum;
}
 m, n, and sum are all local variables
 The scope of m, n, and sum is the method
 These variables can only be used in the method, nowhere else
 The names can be re-used elsewhere, for other variables
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
7
Declarations in a method
 The scope of formal parameters is the entire method
 The scope of a variable in a block starts where you
define it and extends to the end of the block
if (x > y) {
int larger = x;
}
else {
int larger = y;
}
return larger;
larger
scope of larger
larger
scope of a
different larger
Illegal: not declared in current scope
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
8
Nested scopes
int fibonacci(int limit) {
int first = 1;
int second = 1;
while (first < 1000) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
System.out.println( );
} limitfirst
next
second
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Returning a result from a method
 If a method is to return a result, it must specify the type
of the result:
 boolean isAdult ( …
 You must use a return statement to exit the method
with a result of the correct type:
 return age >= magicAge;
9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Returning no result from a method
 The keyword void is used to indicate that a method
doesn’t return a value
 The return statement must not specify a value
 Example:
 void printAge(String name, int age) {
System.out.println(name + " is " + age + " years old.");
return;
}
 There are two ways to return from a void method:
 Execute a return statement
 Reach the closing brace of the method
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
11
Sending messages to objects
 We don’t perform operations on objects, we “talk” to them
 This is called sending a message to the object
 A message looks like this:
object.method(extra information)
• The object is the thing we are talking to
• The method is a name of the action we want the object to take
• The extra information is anything required by the method in order
to do its job
 Examples:
g.setColor(Color.pink);
amountOfRed = Color.pink.getRed( );
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
12
Putting it all together
class Person {
// fields
String name;
int age;
// constructor
Person(String name) {
this.name = name;
age = 0;
}
// methods
String getName() {
return name;
}
void birthday() {
age = age + 1;
System.out.println(
"Happy birthday!");
}
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
13
Using our new class
Person john;
john = new Person("John Smith");
System.out.print (john.getName());
System.out.println(" is having a birthday!");
john.birthday();
 Of course, this code must also be inside a class!
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
14
Diagram of program structure
 A program consists of
one or more classes
 Typically, each class is
in a separate .java file
Program
File File
File
File
Class
Variables
Constructors
Methods
Variables
Variables
Statements
Statements
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
End…
15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

More Related Content

PPT
1207028 634528828886611250
PDF
Object oriented concepts
PDF
Data handling CBSE PYTHON CLASS 11
PDF
IRE- Algorithm Name Detection in Research Papers
PDF
PDF
Python Fundamentals Class 11
PPTX
When to use a structure vs classes in c++
PPT
4. java intro class
1207028 634528828886611250
Object oriented concepts
Data handling CBSE PYTHON CLASS 11
IRE- Algorithm Name Detection in Research Papers
Python Fundamentals Class 11
When to use a structure vs classes in c++
4. java intro class

What's hot (20)

PPTX
SE-IT JAVA LAB OOP CONCEPT
PPT
Object Oriented Programming Concepts using Java
PDF
Pooja Sharma , BCA Third Year
PDF
Java Programming Paradigms Chapter 1
PPT
Encapsulation
PDF
Text Classification, Sentiment Analysis, and Opinion Mining
PPT
Designing A Syntax Based Retrieval System03
PPTX
Encapsulation C++
PDF
Lesson 2.2 abstraction
PPT
Lecture 2
PPTX
Object oriented programming concept
PDF
Recursion CBSE Class 12
PPTX
Data types in java
PDF
Keywords and classes
PDF
Lecture18 structurein c.ppt
PPTX
Chapter 04 object oriented programming
PPTX
Sharbani bhattacharya VB Structures
PPT
Classes cpp intro thomson bayan college
PPTX
Object Oriented Programming Using C++
SE-IT JAVA LAB OOP CONCEPT
Object Oriented Programming Concepts using Java
Pooja Sharma , BCA Third Year
Java Programming Paradigms Chapter 1
Encapsulation
Text Classification, Sentiment Analysis, and Opinion Mining
Designing A Syntax Based Retrieval System03
Encapsulation C++
Lesson 2.2 abstraction
Lecture 2
Object oriented programming concept
Recursion CBSE Class 12
Data types in java
Keywords and classes
Lecture18 structurein c.ppt
Chapter 04 object oriented programming
Sharbani bhattacharya VB Structures
Classes cpp intro thomson bayan college
Object Oriented Programming Using C++
Ad

Similar to Java - Class Structure (20)

PPT
OOP Principles
PPS
Creating classes and applications in java
PPT
Java căn bản - Chapter7
PPT
Chapter 7 - Defining Your Own Classes - Part II
PPTX
Lecture 5.pptx
PPTX
Hemajava
PPTX
Ch.1 oop introduction, classes and objects
PPTX
Lecture 4
PPT
Object and class
PPT
Class & Objects in JAVA.ppt
PDF
oblect oriented programming language in java notes .pdf
PPTX
Polymorphism.pptx
PPT
packages and interfaces
PDF
Basic OOPs Concepts (E-next.in).pdf
PDF
Java methods or Subroutines or Functions
PPTX
03 object-classes-pbl-4-slots
PPTX
03 object-classes-pbl-4-slots
PPT
Defining classes-and-objects-1.0
PPTX
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
OOP Principles
Creating classes and applications in java
Java căn bản - Chapter7
Chapter 7 - Defining Your Own Classes - Part II
Lecture 5.pptx
Hemajava
Ch.1 oop introduction, classes and objects
Lecture 4
Object and class
Class & Objects in JAVA.ppt
oblect oriented programming language in java notes .pdf
Polymorphism.pptx
packages and interfaces
Basic OOPs Concepts (E-next.in).pdf
Java methods or Subroutines or Functions
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
Defining classes-and-objects-1.0
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Ad

More from Victer Paul (13)

PDF
OOAD - UML - Sequence and Communication Diagrams - Lab
PDF
OOAD - UML - Class and Object Diagrams - Lab
PDF
OOAD - Systems and Object Orientation Concepts
PDF
Java - Strings Concepts
PDF
Java - Packages Concepts
PDF
Java - OOPS and Java Basics
PDF
Java - Exception Handling Concepts
PDF
Java - Object Oriented Programming Concepts
PDF
Java - Basic Concepts
PDF
Java - File Input Output Concepts
PDF
Java - Inheritance Concepts
PDF
Java - Arrays Concepts
PDF
Java applet programming concepts
OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
OOAD - Systems and Object Orientation Concepts
Java - Strings Concepts
Java - Packages Concepts
Java - OOPS and Java Basics
Java - Exception Handling Concepts
Java - Object Oriented Programming Concepts
Java - Basic Concepts
Java - File Input Output Concepts
Java - Inheritance Concepts
Java - Arrays Concepts
Java applet programming concepts

Recently uploaded (20)

DOCX
Get More Leads From LinkedIn Ads Today .docx
PDF
The Edge You’ve Been Missing Get the Sociocosmos Edge
DOCX
Buy Goethe A1 ,B2 ,C1 certificate online without writing
PDF
Subscribe This Channel Subscribe Back You
PDF
Instant Audience, Long-Term Impact Buy Real Telegram Members
PDF
25K Btc Enabled Cash App Accounts – Safe, Fast, Verified.pdf
PDF
FINAL-Content-Marketing-Made-Easy-Workbook-Guied-Editable.pdf
PDF
Buy Verified Cryptocurrency Accounts - Lori Donato's blo.pdf
PDF
Presence That Pays Off Activate My Social Growth
PDF
THE ULTIMATE YOUTUBE SHORTS GROWTH......
PDF
Climate Risk and Credit Allocation: How Banks Are Integrating Environmental R...
PDF
The Fastest Way to Look Popular Buy Reactions Today
PDF
StarNetCafeSB2012D3POYNagaworld2-Hotel-Casino-Phnom Entertainment
PDF
Mastering Social Media Marketing in 2025.pdf
PPTX
Developing lesson plan gejegkavbw gagsgf
PDF
TikTok Live shadow viewers_ Who watches without being counted
DOC
ASU毕业证学历认证,圣三一拉邦音乐与舞蹈学院毕业证留学本科毕业证
PDF
How can India improve its Public Diplomacy - Social Media.pdf
PDF
Transform Your Social Media, Grow Your Brand
PPT
memimpindegra1uejehejehdksnsjsbdkdndgggwksj
Get More Leads From LinkedIn Ads Today .docx
The Edge You’ve Been Missing Get the Sociocosmos Edge
Buy Goethe A1 ,B2 ,C1 certificate online without writing
Subscribe This Channel Subscribe Back You
Instant Audience, Long-Term Impact Buy Real Telegram Members
25K Btc Enabled Cash App Accounts – Safe, Fast, Verified.pdf
FINAL-Content-Marketing-Made-Easy-Workbook-Guied-Editable.pdf
Buy Verified Cryptocurrency Accounts - Lori Donato's blo.pdf
Presence That Pays Off Activate My Social Growth
THE ULTIMATE YOUTUBE SHORTS GROWTH......
Climate Risk and Credit Allocation: How Banks Are Integrating Environmental R...
The Fastest Way to Look Popular Buy Reactions Today
StarNetCafeSB2012D3POYNagaworld2-Hotel-Casino-Phnom Entertainment
Mastering Social Media Marketing in 2025.pdf
Developing lesson plan gejegkavbw gagsgf
TikTok Live shadow viewers_ Who watches without being counted
ASU毕业证学历认证,圣三一拉邦音乐与舞蹈学院毕业证留学本科毕业证
How can India improve its Public Diplomacy - Social Media.pdf
Transform Your Social Media, Grow Your Brand
memimpindegra1uejehejehdksnsjsbdkdndgggwksj

Java - Class Structure

  • 1. Java Class Structure Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam 1
  • 2. 2 Classes  A class describes a set of objects  The objects are called instances of the class  A class describes:  Fields (instance variables)that hold the data for each object  Constructors that tell how to create a new object of this class  Methods that describe the actions the object can perform  In addition, a class can have data and methods of its own (not part of the objects)  For example, it can keep a count of the number of objects it has created  Such data and methods are called static  We are avoiding static data and methods for the time being Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 3. 3 Defining a class  Here is the simplest syntax for defining a class: class ClassName { // the fields (variables) of the object // the constructors for the object // the methods of the object }  You can put public, protected, or private before the word class  Things in a class can be in any order (I recommend the above order) Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 4. 4 Defining fields  An object’s data is stored in fields (also called instance variables)  The fields describe the state of the object  Fields are defined with ordinary variable declarations:  String name; Double health; int age = 0;  Instance variables are available throughout the entire class that declares them Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 5. Defining a method  A method has the syntax: return-type method-name(parameters) { method-variables code }  Example: boolean isAdult(int age) { int magicAge = 21; return age >= magicAge; }  Example: double average(int a, int b) { return (a + b) / 2.0; } 5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 6. 6 Methods may have local variables  A method may have local (method) variables  Formal parameters are a kind of local variable  int add(int m, int n) { int sum = m + n; return sum; }  m, n, and sum are all local variables  The scope of m, n, and sum is the method  These variables can only be used in the method, nowhere else  The names can be re-used elsewhere, for other variables Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 7. 7 Declarations in a method  The scope of formal parameters is the entire method  The scope of a variable in a block starts where you define it and extends to the end of the block if (x > y) { int larger = x; } else { int larger = y; } return larger; larger scope of larger larger scope of a different larger Illegal: not declared in current scope Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 8. 8 Nested scopes int fibonacci(int limit) { int first = 1; int second = 1; while (first < 1000) { System.out.print(first + " "); int next = first + second; first = second; second = next; } System.out.println( ); } limitfirst next second Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 9. Returning a result from a method  If a method is to return a result, it must specify the type of the result:  boolean isAdult ( …  You must use a return statement to exit the method with a result of the correct type:  return age >= magicAge; 9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 10. Returning no result from a method  The keyword void is used to indicate that a method doesn’t return a value  The return statement must not specify a value  Example:  void printAge(String name, int age) { System.out.println(name + " is " + age + " years old."); return; }  There are two ways to return from a void method:  Execute a return statement  Reach the closing brace of the method 10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 11. 11 Sending messages to objects  We don’t perform operations on objects, we “talk” to them  This is called sending a message to the object  A message looks like this: object.method(extra information) • The object is the thing we are talking to • The method is a name of the action we want the object to take • The extra information is anything required by the method in order to do its job  Examples: g.setColor(Color.pink); amountOfRed = Color.pink.getRed( ); Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 12. 12 Putting it all together class Person { // fields String name; int age; // constructor Person(String name) { this.name = name; age = 0; } // methods String getName() { return name; } void birthday() { age = age + 1; System.out.println( "Happy birthday!"); } } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 13. 13 Using our new class Person john; john = new Person("John Smith"); System.out.print (john.getName()); System.out.println(" is having a birthday!"); john.birthday();  Of course, this code must also be inside a class! Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 14. 14 Diagram of program structure  A program consists of one or more classes  Typically, each class is in a separate .java file Program File File File File Class Variables Constructors Methods Variables Variables Statements Statements Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 15. End… 15Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam