The class is the basic unit of Java programming. Everything in Java is an object, where each object is of a class type.
A class is a data type definition. The object is a variable of the class type. e.g. String (class).
String s = "This is a string";
Another way is that the class is a category of objects. e.g. Car (class) can have objects My Toyota and Your Ford. These objects are of type Car.
Class Definition Syntax
We have already used:
public class My Program
{
public static void main(String[] args)
{
system.out.println ("Hello World");
}
}
We now use:
public class ClassName
{
//Attributes, variables contained either by the class itself or by objects of the class type.
//Constructors: special methods called when an object is created.
//Other methods: manipulate the attributes.
}
What is public?
This is an access specifier, it determines what other classes can access this resource. Another access specifier is private.
public: anyone can use this resource.
private: only methods within the class itself can access the resource.
There are another two: protected and (blank), i.e. no access specifier.
The ability to make resources private, or specify their access is fundamental to object-oriented programming.
It obscures details, abstracting away complexity, prevents other programs from messing with the internals of the class, and forces other programmers to interact with the class in a manner you specify. This is, in effect, the Application Programming Interface (API).
What is "static"?
A static feature of a class is a feature associated with the class is a feature associated with the class type itself, not with any particular object.
Example, the Car class: all are fueled by Gasoline (static attribute). My Toyota has a fuel level of a quarter tank and Your Ford has a fuel level of a half tank. This "fuel level" is a non-static attribute.
Example, the Jet class: all are fueled by JetA (static attribute). Air Canada Airbus has a fuel level of a full tank and Bill Gates' Private Jet has a fuel level of three-quarters of a tank. This "fuel level" is a non-static attribute.
Declaration of Access Specifiers
(access specifier) static (type of attribute) (attribute name) = initialized value;
Examples: public static String fuel = "Gasoline";
public static int x = 0;
public static double y = 1.2;
In the fuel example, the type of fuel will never change. However fuel is a public attribute that can be changed by other classes. We therefore use the final keyword to ensure this attribute is not writable , and the initial value becomes mandatory.
public static final String fuel = "Gasoline";
Static constructor?
Makes no sense, there's no such thing.
Static method?
Just fine. Write static after the access specifier as in public static void main(...)
They cannot access non-static attributes of an object.
Utility class
A class where every feature is static.
Here is a temporary link to a test video by Louis.
{
public static void main(String[] args)
{
system.out.println ("Hello World");
}
}
We now use:
public class ClassName
{
//Attributes, variables contained either by the class itself or by objects of the class type.
//Constructors: special methods called when an object is created.
//Other methods: manipulate the attributes.
}
What is public?
This is an access specifier, it determines what other classes can access this resource. Another access specifier is private.
public: anyone can use this resource.
private: only methods within the class itself can access the resource.
There are another two: protected and (blank), i.e. no access specifier.
The ability to make resources private, or specify their access is fundamental to object-oriented programming.
It obscures details, abstracting away complexity, prevents other programs from messing with the internals of the class, and forces other programmers to interact with the class in a manner you specify. This is, in effect, the Application Programming Interface (API).
What is "static"?
A static feature of a class is a feature associated with the class is a feature associated with the class type itself, not with any particular object.
Example, the Car class: all are fueled by Gasoline (static attribute). My Toyota has a fuel level of a quarter tank and Your Ford has a fuel level of a half tank. This "fuel level" is a non-static attribute.
Example, the Jet class: all are fueled by JetA (static attribute). Air Canada Airbus has a fuel level of a full tank and Bill Gates' Private Jet has a fuel level of three-quarters of a tank. This "fuel level" is a non-static attribute.
Declaration of Access Specifiers
(access specifier) static (type of attribute) (attribute name) = initialized value;
Examples: public static String fuel = "Gasoline";
public static int x = 0;
public static double y = 1.2;
In the fuel example, the type of fuel will never change. However fuel is a public attribute that can be changed by other classes. We therefore use the final keyword to ensure this attribute is not writable , and the initial value becomes mandatory.
public static final String fuel = "Gasoline";
Static constructor?
Makes no sense, there's no such thing.
Static method?
Just fine. Write static after the access specifier as in public static void main(...)
They cannot access non-static attributes of an object.
Utility class
A class where every feature is static.
Here is a temporary link to a test video by Louis.
2 comments:
Thank you Philip:)
You're welcome Mr. Vito.
I shall have Friday's and tomorrow's lecture up after the lecture. As you can imagine, I had a big weekend ;).
Post a Comment