Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

4.java Class and Methods

The document discusses the key concepts of classes, objects, and methods in Java programming. It defines a class as a template for objects that defines their data and behaviors. An object is an instance of a class that allocates memory at runtime. Methods define reusable blocks of code that can be executed on objects. The document provides examples of how to define classes with data members and methods, as well as how to create objects and call methods.

Uploaded by

srocky4life
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

4.java Class and Methods

The document discusses the key concepts of classes, objects, and methods in Java programming. It defines a class as a template for objects that defines their data and behaviors. An object is an instance of a class that allocates memory at runtime. Methods define reusable blocks of code that can be executed on objects. The document provides examples of how to define classes with data members and methods, as well as how to create objects and call methods.

Uploaded by

srocky4life
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

JAVA Programming Language

class , Object& methods


class

• Java is an object-oriented programming language, so everything in java


program must be based on the object concept.
• class concept defines the skeleton of an object.
• The java class is a template of an object. It defines the blueprint of an object.
• Once a class got created, we can generate as many objects as we want.
• Every class of java programming language has the following characteristics.
o Identity - It is the name given to the class.
o State - Represents data values that are associated with an object.(data)
o Behavior - Represents actions can be performed by an object.(methods)
class
• Class is a template for the object, which has own data and methods.
• Creating a Class

class ClassName
{
data members declaration;
methods defination;
}

• The ClassName must begin with an alphabet, and the Upper-case letter is
preferred.
we must follow all naming rules.
class
• Java Class
o User defined class(Sample, Add, Student…etc)
o Pre defined/built-in class(Scanner,System,Thread…etc)
Object
• An object is an instance of type class.
• When an object of a class is created, the class is said to be instantiated.
• Objects created using a single class have the same properties and
methods of that class.
• We can create any no of Object for a class
• The value of properties is different for every object
• Syntax – Object creation
• ClassName objectName= new ClassName( );

new operator allocates memory space for objects


Java Methods

• A method is a block of statements used to perform a specific task.


• A method get executed, when it is called.
• Syntax
[access Modifier] returnType methodName( [parameters] )
{
... block of statements; ...
}
 We will call a method using the name of the method
• returnType - Specifies the data type of a return value.(void,int,..)
• name - Specifies a unique name to identify it.(except Keywords)
• parameters - The data values it may accept or receive.
• { } - Defines the block belongs to the method.
Example 11 – class, objects and Methods
Example 12- Methods with arguments
Example 13 – return keyword
Example 14
How Many objects created?
class Sample
{
int a;
void get(int x)
{
a=x;
System.out.println("The Value is "+a);
}
}
public class Main
{
public static void main(String a[])
{
Sample s1=new Sample();
Sample s2=new Sample();// new operator create memory space for objects
Sample s3=s2;
s1.get(10);
s2.get(20);
s3.get(30);
//System.out.println(s2.a);
}
}
class Sample
{
int a;
void get(int a)//local variable
{
a=a+a;
System.out.println("The Value is "+a);
}
}
public class Main
{
public static void main(String a[])
{
Sample s1=new Sample();
Sample s2=new Sample();
s1.get(10);
s2.get(20);
System.out.println(s1.a);
System.out.println(s2.a);
}
}
class Sample
{
int a;
void get(int a)//local variable
{
this.a=a+a;
System.out.println("The Value is "+this.a);
}
}
public class Main
{
public static void main(String a[])
{
Sample s1=new Sample();
Sample s2=new Sample();
s1.get(10);
s2.get(20);
System.out.println(s1.a);
System.out.println(s2.a);
}
}
Thank you

You might also like