Professional Documents
Culture Documents
LECTURE THREE
INTRODUCTION TO JAVA
By using standard Java naming conventions, your code is easier to read and less time is spent to figure out what
the code does.
Name Convention
class name should start with uppercase letter and be a noun e.g. String, Color, Button, System,
Thread etc.
interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote,
ActionListener etc.
method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(),
println() etc.
variable name should start with lowercase letter e.g. firstName, orderNumber etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.
Constant name should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;
/* constructor , a constructor that specifies how these fields are initialized when an Entry object is constructed*/
Entry(String n, String a, String p) {
this.name = n;
this.address = a;
this.phone = p;
}
}
1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724
Constructor overloading
Like methods, constructors can be overloaded. In other words, you can provide more than
one constructor for a class if each constructor has a unique signature. Here’s another
constructor for the Actor class. A constructor in Java is the initiator of an object; anytime
you create a new instance of a class, a constructor is invoked. If you do not create
a constructor, the default constructor (no arguments, no other real code) is created for
you by Java.
class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;
/* constructor , a constructor that specifies how these fields are initialized when an Entry object is constructed*/
Entry(String n, String a, String p) {
this.name = n;
this.address = a;
this.phone = p;
}
/* another constructor , a constructor that specifies how these fields are initialized when an Entry object is
constructed*/
Entry(String n, String a) {
this.name = n;
this.address = a;
}
}
1/17/2021 Thobius Joseph(Msc in Eng.), 0783758724
Default Constructor
If you do not create any constructor, the java will create constructor automatically for you.
This constructor is called default constructor . The default constructor is the constructor
with no arguments and no other real code.
If you create any constructor inside your code then java will not create default constructor
for you. In this case if you need constructor which look like default you must write by
yourself.
Example if you write this class
class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;
}
Then the java will create default constructor for you internal such that you wont see it
class Entry {
/* fields, A field is a ``container'' that holds a value*/
String name;
String address;
String phone;
Entry (){ // default constructor is created automatically inside your class therefore you wont see it
}
}
Instance variables
- Instance variables are variables within a class but outside any method. These variables are
initialized when the class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
- A variable declared inside the class but outside the body of the method, is called instance
variable. It is not declared as static. It is called instance variable because its value is instance
specific and is not shared among instances.
Class (or static) variables
- Class variables are variables declared within a class, outside any method, with the static
keyword
- Unlike instance variables, we can only have one copy of a static variable per class
irrespective of how many objects
1/17/2021
we create
Thobius Joseph(Msc in Eng.), 0783758724
Variables
Examples
class A {
int data=50;//instance variable
void method() {
int n=90;//local variable
}
}//end of class
Changes made in an instance variable using one object will not be reflected in other
objects as each object has its own copy of instance variable. In case of static, changes will
be reflected in other objects as static variables are common to all object of a class.
We can access instance variables through object references and Static Variables can be
accessed directly using class name.
class Example {
static int a; //static variable
int b; //instance variable
}
Class declared as final :: Class declared Method declared as final :: A method declared as final
as final cannot be overridden; this means even when a child
When a class is declared as final, it class can call the final method of parent class without
cannot be extended further. Provide any issues, but the overriding will not be possible.
Here to a class an example what Here is a sample program showing what is not valid
happens within a program within a Java program when a method is declared as
final.
Once a variable is assigned with the keyword final, it always contains the same exact value. Again things may
happen like this; if a final variable holds a reference to an object then the state of the object can be altered if
programmers perform certain operations on those objects, but the variable will always refer to the same object.
A final variable that is not initialized at the time of declaration is known as a blank final variable. If you are
declaring a final variable in a constructor, then you must initialize the blank final variable within the constructor
of the class. Otherwise, the program might show a compilation error.