Data Members And Methods
Types of Data Members: -
We know that a class is a collection of data members and methods. In java programming we have two types of data members they are
1. Instance/non-static data members
2. Static data members
1. Instance/non-static data members
2. Static data members
Instance Data members
1. Instance data members are those whose memory space is created each and every time whenever an object is created.
2. Instance data members are always meant for storing specific values. 3. Programmatically instance data members declaration should not be preceded by a keyword static. Syntax: - Data type v1, v2, v3 ………. Vn; 4. Each and every instance data member must be access with respective object name. Ex: - Objname.instance data member name 5. Instance data members are also known as Object Level Data Members because they depend on object name and independent from class name. Ex: - int pin; int stno; string sname; |
Static Data members
1. Static data members are those whose memory space is created only once, whenever the class is loaded in the main memory irrespective of no of objects are created.
2. Static data members are meant for storing common values. 3. Programmatically static data member declaration must be preceded by static keyword. Syntax: - static data type v1, v2, v3 ………. Vn; 4. Each and every static data member must be access with respective class name. Ex: - Class name.static data member name 5. Static data member are also known as Class Level Data Members because they depends on class name and independent from object name Ex: - static String cname; static String CapIndia; |
Note: -
Each and every final variable value must be static but a static variable value may (or) may not be final.
Ex: -
1. static final float PI = 3.1417;
2. static String crs name =”core java”
crs name= “Adv java”;
Each and every final variable value must be static but a static variable value may (or) may not be final.
Ex: -
1. static final float PI = 3.1417;
2. static String crs name =”core java”
crs name= “Adv java”;
Types of Methods: -
We know that each and every method is meant for performing some operation. In java programming we have two types of methods they are
1. Instance/ non –static methods
2. Static methods
1. Instance/ non –static methods
2. Static methods
Instance methods: -
1. Instance methods are always recommended to perform repeated operations like
reading records from the file, reading records from the data base etc…
2. Programmatically instance methods definitions should not be preceded by a
keyword static.
Syntax: -
Return type method name (list of parameters if any)
{
Block of statements;
}
3. Each and every instance method must be access with respective Object name.
4. Result of instance method is not sharable
Ex: -
void getTotal()
{
Tot= m1+ m2+ m3;
}
1. Instance methods are always recommended to perform repeated operations like
reading records from the file, reading records from the data base etc…
2. Programmatically instance methods definitions should not be preceded by a
keyword static.
Syntax: -
Return type method name (list of parameters if any)
{
Block of statements;
}
3. Each and every instance method must be access with respective Object name.
4. Result of instance method is not sharable
Ex: -
void getTotal()
{
Tot= m1+ m2+ m3;
}
Static methods: -
1. Static methods are always which are recommended to perform one operation like
opening the files, obtaining a data base connection etc…
2. Programmatically static methods definitions must be preceded by static keyword.
Syntax: -
Static Return type method name (list of parameters if any)
{
Block of statements;
}
3. Each and every static method must be accessed with respect class name.
4. Result set of static method always sharable
Ex: -
Public static void main ()
{
S1.getTotal();
S2.getTotal();
}
1. Static methods are always which are recommended to perform one operation like
opening the files, obtaining a data base connection etc…
2. Programmatically static methods definitions must be preceded by static keyword.
Syntax: -
Static Return type method name (list of parameters if any)
{
Block of statements;
}
3. Each and every static method must be accessed with respect class name.
4. Result set of static method always sharable
Ex: -
Public static void main ()
{
S1.getTotal();
S2.getTotal();
}
Note: -
A class of java contains instance data members, static data members and instance methods static methods. Instance data members and instance methods will be access with respective to <object name>. Static data members and static methods will be access with respective to <class name>.
System.out.println (-) ===== > 1
System.out.print (-) ===== > 2
Statement 1 is used for displaying the result of java program on the console (monitor) line by line.
Statement 2 is used for displaying the result of java program on the console (monitor) in the same line.
A class of java contains instance data members, static data members and instance methods static methods. Instance data members and instance methods will be access with respective to <object name>. Static data members and static methods will be access with respective to <class name>.
System.out.println (-) ===== > 1
System.out.print (-) ===== > 2
Statement 1 is used for displaying the result of java program on the console (monitor) line by line.
Statement 2 is used for displaying the result of java program on the console (monitor) in the same line.
Technical Description: -
1. Println () and print () of the two pre-defined instance methods presenting a pre-
1. Println () and print () of the two pre-defined instance methods presenting a pre-
defined class called PrintStream.
2. To access these pre-defined instance methods we require as object of
2. To access these pre-defined instance methods we require as object of
PrintStream class.
3. An object of PrintStream called out created by sun micro system as the static data
3. An object of PrintStream called out created by sun micro system as the static data
member in another pre-defined class called System.
Hence println () and print () must be access as follows
System.out.println(-)
System.out.print(-)
Let us consider the following skeleton code development for
System.out.println()
System.out.print(-)
Hence println () and print () must be access as follows
System.out.println(-)
System.out.print(-)
Let us consider the following skeleton code development for
System.out.println()
System.out.print(-)
Class PrintStream
{
Public void println()
{
-----------
-----------
}
Public void println()
{
-----------
-----------
}
}
Class System
{
static printStream out = new PrintStream();
}
Ex: -
1. Hello Java world
System.out.println(“Hello java World” );
2. int a = 10;
System.out.println(a); // 10
System.out.println(“value of a =”+a); // value of a = 10
System.out.println(a+”is the value of a”); // 10 is the value of a
3. int a=10, b=20;
int c;
c= a+b;
System.out.println(“sum =”+c) //sum = 30
System.out.println(c+”is the sum”) //30 is the sum
System.out.println(“sum of” +a+ ”and” +b+ ”=” +c);
// sum of 10 and 20 = 30
Visitors