A variable in Java has a type, name and a value. A variable may store of value of either:
- a primitive data type such as int, boolean, char.
or
- a reference to an object, also called reference variable.
for example:
int i; //variable i that can store an int value.
boolean isDone; //Variable isDone that can store a boolean value.
//Variables a, f and r that can store a char value each:
char a,f,r;
//Equivalent to the three separate declarations:
char a;
char f;
char r;
What we just deed above is called variable declaration. By declaring variable we are implicitly allocating memory for these variables and determining the value types that can be stored in them.
So, in the example above, we named a variable: isDone that can store a boolean value, but not initialized yet.
Giving a variable a value when declared is called initialization. Further, we can declare and initialize a variable in one go. For example, we could have declared and initialized our variables of the previous example:
int i = 101; //variable i initialized with the value 101:
//variable isDone initialized with the boolean value true.
boolean isDone = true;
// variables a,f and r initialized with the values 'a','f' and 'r' //respectively:
char a = 'a', f='f', r='r';
// equivalent to:
char a = 'a';
char f = 'f';
char r = 'r';
Analogous to declaring variables to denote primitive values, we can declare a variable denoting an object reference, that's; a variable having one of the following reference types: a class, an array, or an interface.
For instance (see also Class Instantiation ):
//declaring a redCar and a blueCar of class Car.
Car redCar, blueCar;
Please note that declarations above do not create any object of type Car. These are just variables that can store references to objects of class Car but no reference is created yet.
A reference variable has to be instantiated before being used. So:
// declaring and initializing the redCar reference variable
Car redCar=new Car("red");
An object of class Car is created using the keyword new together with the Constructor call Car("red"), then stored in the variable redCar which is now ready to be manipulated.
Showing posts with label Java Objects. Show all posts
Showing posts with label Java Objects. Show all posts
Java Class Instantiation
The process of creating objects from a class is called instantiation. So an object is always an instance of a class which represents the blueprint.
The object is constructed using the class and must be created before being used in a program.
Objects are manipulated through object references (also called reference values or simply references)
Creating objects in Java usually follow these steps:
1- Declaration of a reference variable of the appropriate class which will store a reference to the object.
For example:
2- Creating an object
This involves using the new operator and calling a constructor to create an instance of the class.
The new operator returns a reference to a new instance of the Car class.
The reference can be assigned to a reference variable of the appropriate class, here: myCar and myFatherCar.
Each object has a unique identity and has its own copy of the fields declared in the class.
The purpose of calling the constructor on the right side of the new operator is to initialize the newly created object.
The declaration and initialization can be combined:
The object is constructed using the class and must be created before being used in a program.
Objects are manipulated through object references (also called reference values or simply references)
Creating objects in Java usually follow these steps:
1- Declaration of a reference variable of the appropriate class which will store a reference to the object.
For example:
//declaring my carOr combined if they belong to the same appropriate class, separated by a comma:
Car myCar;
// declaring my father's car
Car myFatherCar ;
//declaring my car and my father's car
Car myCar, myFatherCar ;
2- Creating an object
This involves using the new operator and calling a constructor to create an instance of the class.
The new operator returns a reference to a new instance of the Car class.
The reference can be assigned to a reference variable of the appropriate class, here: myCar and myFatherCar.
// instantiating myCar from the class Car,
//having the String "black" as parameter value.
myCar = new Car("black");
// instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
myFatherCar = new Car("blue");
Each object has a unique identity and has its own copy of the fields declared in the class.
The purpose of calling the constructor on the right side of the new operator is to initialize the newly created object.
The declaration and initialization can be combined:
// declaring and instantiating myCar from the class Car,
//having the String "black" as parameter value.
Car myCar = new Car("black");
// declaring and instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
Car myFatherCar = new Car("blue");
Abstractions, Class and Object. What relation?
Suppose you were asked to describe your lovely car you just bought. You would certainly mention the most important properties according to you.
You would, for instance, stress out the high speed it can reach if you are one of the speedy-cars geeks. Or how spacious it is if you are blessed with one of those large families. Just out-of-university engineer, you would talk about every piece its motor contains and how aerodynamics rules would have been better applied to gain more speed. Or greeny activist, you would just mention the electrical-hybrid motor it contains and its befits for your backyard garden.
you got it :)
Discarding unimportant properties or parts while describing your car, is called abstractions. So, for this simple car example, many abstractions may be done depending on your mechanical skills, visual importance, financial side and many other considerations. Abstraction of the same car differ, hence solving the same problem would practically lead to different solutions.
Abstraction is a tricky and diversion-prone step when developing software.
Java is an Object oriented Programming language(OOP), that means any programming problem can be modeled as one or more objects. So an object models an abstraction by defining the properties and functions making that object different from others. You abstract your car as an object with red-color property and letting you reach you work smoothly each day, as a function. Or as an object being of those luxury Ferari's and having that ghost-oriented shape as properties while operating as a social status need.
let's Java a bit:
Properties of an object are also called attributes and are defined in Java with fields. Functions, also known as operations, are called methods.
Collectively fields and methods in a class are called members.
We may categorize objects to the same class: Car is the class of all cars in our example. nicely this matches the Java naming: class
Let's recapitulate:
Java is an OOP language. A class defines a category of objects. Fields and methods are defined by a class which acts as a footprint for its objects.
Now, let's create our first Java class and give it the name Car:
From now on I'll write comments within the code itself. // denotes that this code line is a comment. Comments within Java code are for human use and are ignored by the compiler.
Generally, a method may have any arbitrary name, by convention, it is a verb or verbal phrase ( ex: changeColor(), startEngin()), followed by its zero or more parameters between enclosing parentheses ( ). Parameters in our example are: String carColor and String newColor.
String is a Java keyword denoting a type. initialColor and newColor are arbitrary parameter names you may choose freely.
As you can see, the method Car(String carColor) has the same name as the class name. In Java, this special method is called a Constructor and is used to create (instantiate, see post) objects from the class.
In this class, we declare one instance field. We choose its name to be initialColor denoting a variable that will hold the car color value. Its type is String.
String carColor; is called in Java a declaration. The semicolon at the end is mandatory indicating the end of the declaration;
Generally, you may declare as many fields as you wish.
see u next time :)
You would, for instance, stress out the high speed it can reach if you are one of the speedy-cars geeks. Or how spacious it is if you are blessed with one of those large families. Just out-of-university engineer, you would talk about every piece its motor contains and how aerodynamics rules would have been better applied to gain more speed. Or greeny activist, you would just mention the electrical-hybrid motor it contains and its befits for your backyard garden.
you got it :)
Discarding unimportant properties or parts while describing your car, is called abstractions. So, for this simple car example, many abstractions may be done depending on your mechanical skills, visual importance, financial side and many other considerations. Abstraction of the same car differ, hence solving the same problem would practically lead to different solutions.
Abstraction is a tricky and diversion-prone step when developing software.
Java is an Object oriented Programming language(OOP), that means any programming problem can be modeled as one or more objects. So an object models an abstraction by defining the properties and functions making that object different from others. You abstract your car as an object with red-color property and letting you reach you work smoothly each day, as a function. Or as an object being of those luxury Ferari's and having that ghost-oriented shape as properties while operating as a social status need.
let's Java a bit:
Properties of an object are also called attributes and are defined in Java with fields. Functions, also known as operations, are called methods.
Collectively fields and methods in a class are called members.
We may categorize objects to the same class: Car is the class of all cars in our example. nicely this matches the Java naming: class
Let's recapitulate:
Java is an OOP language. A class defines a category of objects. Fields and methods are defined by a class which acts as a footprint for its objects.
Now, let's create our first Java class and give it the name Car:
class Car {class is a Java keyword, it is mandatory and put just before the class name (here, Car). The class scope begin with { and ends with }. They are both mandatory.
}
From now on I'll write comments within the code itself. // denotes that this code line is a comment. Comments within Java code are for human use and are ignored by the compiler.
class Car { // begin of the class scopeCar(String initialColor) and changeColor(String newColor) are both Java methods. As with the class, { and } define method's scope.
// The constructor. having one String parameter: initialColor
public Car(String initialColor)
{
// Still empty method body for now.
} // end of the constructor scope
// Another method with one String parameter: newColor
public void changeColor(String newColor)
{ //begin of changeColor method scope
// Still empty method body for now.
} //end of this method scope
} // end of the class scope
Generally, a method may have any arbitrary name, by convention, it is a verb or verbal phrase ( ex: changeColor(), startEngin()), followed by its zero or more parameters between enclosing parentheses ( ). Parameters in our example are: String carColor and String newColor.
String is a Java keyword denoting a type. initialColor and newColor are arbitrary parameter names you may choose freely.
As you can see, the method Car(String carColor) has the same name as the class name. In Java, this special method is called a Constructor and is used to create (instantiate, see post) objects from the class.
class Car
{
// carColor variable declaration
String carColor ;
// The constructor method
public Car(String initialColor) {
// The value of initialColor variable
// is assigned to carColor variable.
carColor = initialColor ;
}
public void changeColor(String newColor )
{
// carColor variable is assigned the value of newColor
carColor = newColor;
}
}
In this class, we declare one instance field. We choose its name to be initialColor denoting a variable that will hold the car color value. Its type is String.
String carColor; is called in Java a declaration. The semicolon at the end is mandatory indicating the end of the declaration;
Generally, you may declare as many fields as you wish.
see u next time :)
Subscribe to:
Posts (Atom)