Professional Documents
Culture Documents
Introduction To JAVA - 3
Introduction To JAVA - 3
Introduction To JAVA - 3
LECTURE THREE
INTRODUCTION TO JAVA
3. Object
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;
}
}
02/21/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;
}
}
02/21/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
}
}