II BCA - CORE PAPER 3
PROGRAMMING IN JAVA
UNIT 1
DEFINE CLASS
Java Class
·
A Java
Class is like a wrapper or envelope that comprises a number of Methods and Variables.
·
A
class defines the properties and behavior of an object.
·
In
Java class, the variables represent the properties of the object and the
methods that represent the behavior of the object.
·
A Java program consists of a CLASS with
some Methods & Variable.
Java Class Syntax
Java classes defined in the following
way:
class MyClass {
// fields,
// constructor, and
// method declarations
}
Java class declarations can include the
following components:
1.
Modifiers such
as public, private (the private modifier
can only be applied to Nested Classes.)
2.
The class name,
with the initial letter capitalized by convention.
3.
The name of the
class's parent (superclass), if any, preceded by the keyword extends.
A class can only extend (subclass) one parent.
4.
A comma-separated
list of interfaces implemented by the class, if any, preceded by the
keyword implements. A class can implement more
than one interface.
5.
The class body,
surrounded by braces, {}.
Java Source File Declaration Rules
The rules associated with declaring
classes, import statements, and package statements in a source file are,
·
There can be only one public class per source code file.
·
Comments can appear at the beginning or end of any line
in the source code file; they are independent of any of the positioning rules
discussed here.
·
If there is a public class in a file, the name of the file must
match the name of the public class. For example, a class declared as public class
Dog { } must be in a
source code file named Dog.java.
·
If the class is part of a package, the package statement must
be the first line in the source code file, before any import statements that
may be present.
·
If there are import statements, they must go between the package statement (if
there is one) and the class declaration. If there isn't a package statement, then
the import statement(s)
must be the first line(s) in the source code file. If there are no package or import
statements, the class declaration must be the first line in the source code
file.
·
Import and package statements apply to all classes within
a source code file. In other words, there's no way to declare multiple classes
in a file and have them in different packages or use different imports.
·
A file can have more than one nonpublic class.
·
Files with no public classes can have a name that does
not match any of the classes in the file.
Compiling with
javac
·
The javac command is used to invoke
Java's compiler. You can specify many options when running javac.
·
For example, there are options to generate debugging information or
compiler warnings.
·
Here's the structural overview for javac:
o javac [options] [source
files]
Example:
javac MyClass.java
Launching Applications with java
·
The java command is used to invoke
the Java Virtual Machine (JVM).
·
Here's the basic structure of the
command:
o java [options] class
[args]
Example:
java
MyClass
Using public static
void main(String[ ] args)
·
main() is the
method that the JVM uses to start execution of a Java program.
·
As far as the compiler and the JVM are concerned, the only version of main() with superpowers is the main() with this signature:
o
public static void main(String[] args)
Import Statements
and the Java API
·
The import statement is
used to bring certain classes or the entire packages, into visibility. As soon
as imported, a class can be referred to directly by using only its name.
·
The import statement
is a convenience to the programmer and is not technically needed to write
complete Java program. If you are going to refer to some few dozen classes into
your application, the import statement will save a lot of time
and typing also.
·
In a Java source file, the import statements
occur immediately following the package statement (if exists)
and before any class definitions.
·
Below is the general form of
the import statement :
import pkg1 [.pkg2]. (classname|*);
·
Here, pkg1 Ã the name of top-level package,
and
·
pkg2 Ã the name of subordinate package
inside outer package separated by dot (.).
·
classname or a star (*), indicates that the Java compiler should
import the full package.
Example:
import java.util.Date;
import java.io.*;
·
All the basic Java classes included
with Java are stored in a package called java.
·
The fundamental language functions
are stored in a package inside of the java package i.e., java.lang.
import java.lang.*;
Static Import Statements
·
The
static import feature allows the java programmer to access any static member of
a class directly.
·
For Example: we always use sqrt() method of Math class by
using Math class i.e. Math.sqrt(), but by using static import we can access sqrt() method
directly.
The rules for using static imports:
·
You must say import static; you can't say
static import.
·
Watch out for ambiguously named static members. For
instance, if you do a static import for both the Integer class and the Long class,
referring to MAX_VALUE will cause a
compiler error, since both Integer and Long have a MAX_VALUE constant, and Java won't know which MAX_VALUE you're
referring to.
·
You can do a static import on static object
references, constants and static methods.
Example:
// Java
Program to illustrate static import statement
import
static java.lang.Math.*;
class Mtest
{
public static void main(String[] args)
{
System.out.println(sqrt(9));
//3
System.out.println(pow(2, 3));//8
}
}
Advantage of static import:
- Less
coding is required if you have access any static member of a class often.
Disadvantage of static
import:
- If
you overuse the static import feature, it makes the program unreadable and
unmaintainable.