7-A Closer Look at Classes and Methods-2
7-A Closer Look at Classes and Methods-2
}
public class Number_Objects
{
static int count=0;
Number_Objects()
{
count++;
}
public static void main(String[] args)
{
Number_Objects obj1 = new Number_Objects();
Number_Objects obj2 = new Number_Objects();
Number_Objects obj3 = new Number_Objects();
Number_Objects obj4 = new Number_Objects();
System.out.println("Number of objects created:"+count);
}
}
Final keyword
• A variable can be declared as final. Doing so prevents its contents
from being modified. This means that we must initialize a final
variable when it is declared. (In this usage, final is similar to const in
C/C++.)
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
• Variables declared as final do not occupy memory on a per- instance
basis. (final variable uses single memory location which is shared by
all instance/objects.). So a final variable is essentially a constant.
• The keyword final can also be applied to methods, but its meaning is
substantially different than when it is applied to variables. (related
with inheritance.)
Arrays
• Array are implemented as objects. Because of
this, there is a special array attribute that we
can take advantage of. Specifically, the size of
an array—that is, the number of elements that
an array can hold—is found in its length
instance variable. All arrays have this variable,
and it will always hold the size of the array.
// This program demonstrates the length array member.
class Length {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
int a3[] = {4, 3, 2, 1};
System.out.println("length of a1 is " + a1.length);
System.out.println("length of a2 is " + a2.length);
System.out.println("length of a3 is " + a3.length);
}
}
This program displays the following output:
length of a1 is 10
length of a2 is 8
length of a3 is 4
Nested and Inner Classes
• It is possible to define a class within another class; such
classes are known as nested classes.
• The scope of a nested class is bounded by the scope of
its enclosing class. Thus, if class B is defined within class
A, then B is known to A, but not outside of A. A nested
class has access to the members, including private
members, of the class in which it is nested. However, the
enclosing class does not have access to the members of
the nested class.
[Inner class can access all members of outer class but
outer class can’t access member of inner class.]
• There are two types of nested classes:
static and non-static.
• A static nested class is one which has the static modifier
applied. Because it is static, it must access the members
of its enclosing class through an object. That is, it cannot
refer to members of its enclosing class directly. Because
of this restriction, static nested classes are seldom used.
• The most important type of nested class is the inner
class. An inner class is a non-static nested class. It has
access to all of the variables and methods of its outer
class and may refer to them directly in the same way that
other non-static members of the outer class do. Thus, an
inner class is fully within the scope of its enclosing class.
// Demonstrate an inner class.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
} } }
class InnerClassDemo {
public static void main(String args[]) { //Output :
Outer outer = new Outer(); //display: outer_x = 100
outer.test();
} }
• In the program, an inner class named Inner is defined within
the scope of class Outer. Therefore, any code in class Inner can
directly access the variable outer_x.
• An instance method named display( ) is defined inside Inner.
This method displays outer_x on the standard output stream.
• The main( ) method of InnerClassDemo creates an instance of
class Outer and invokes its test( ) method. That method creates
an instance of class Inner and the display( ) method is called.
• class Inner is known only within the scope of class Outer.
• The Java compiler generates an error message if any code
outside of class Outer attempts to instantiate class Inner.
• Generalizing, a nested class is no different than any other
program element: it is known only within its enclosing
scope.
• An inner class has access to all of the members of its
enclosing class, but the reverse is not true.
• Members of the inner class are known only within the
scope of the inner class and may not be used by the outer
class.
*Nested classes were not allowed by the original 1.0
specification for Java. They were added by Java 1.1.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
int y = 10; // y is local to Inner
void display() {
System.out.println("display: outer_x = " + outer_x);
} }
void showy() {
System.out.println(y); // error, y not known here!
} }
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}}
The String Class
• Every string you create is actually an object of type String.
Even string constants are actually String objects. For example,
in the statement
System.out.println("This is a String, too");
the string “This is a String, too” is a String constant.
Fortunately, Java handles String constants in the same way
that other computer languages handle “normal” strings.
• The second thing to understand about strings is that objects
of type String are immutable; once a String object is created,
its contents cannot be altered. While this may seem like a
serious restriction, it is not, for two reasons:
1. If you need to change a string, you can always create a new
one that contains the modifications.
2. Java defines a peer class of String, called StringBuffer, which
allows strings to be altered, so all of the normal string
manipulations are still available in Java.
// Demonstrating Strings.
class StringDemo {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;
System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);
The output produced by this program is shown
here:
} First String
Second String
} First String and Second String
Some methods in string class
• s1. charAt(n);
• s2=s1.toLowerCase();
• s2=s1.toUpperCase();
• s2=s1.replace(‘x’,’y’);
• s2=s1.trim();
• s1.equals(s2);
• s1.equalsIgnoreCase(s2);
• s1.length();
• s1.compareTo(s2);
• s1.concat(s2);
• s1.substring(n);
• s1.substring(n,m);
• s1.indexOf(‘x’);
String and command line args
// Display all command-line arguments.
class CommandLine {
public static void main(String args[]) {
for(int i=0; i<args.length; i++)
System.out.println("args[" + i + "]: " +
args[i]);
}
}
Execute the code by --->>java CommandLine this is a test 100 -1
output:
args[0]: this
args[1]: is
args[2]: a
args[3]: test
args[4]: 100
args[5]: -1
Variable number of arguments
• Beginning with JDK 5,(v1.5), java has included a
feature to take variable number of arguments.
• A sample method is
void vaTest(int …v);
which can take any number of integer type
arguments. The method can be overloaded for
other types as well. Here v is implicitly declared as
an array without specifying its size.
class A
{
static void vatest(int ...v)
{
System.out.print("Number of args:" + v.length + " contents: ");
for(int x : v)
System.out.print(x+" ");
System.out.println();
}
public static void main(String args[])
{
vatest(10);
vatest(1,2,3);
vatest();
}
}