From the a class (HungTest) below:
Classes are a mechanism used to group data (variables) and Java code (methods) together into
coherent "modules".
A class has 3 features:
A constructor: HungTest(String aName) is used to construct new objects of this class.
=> Constructor is the method which name is same to the class, also is used to construct an object and initialise data members. It can be with or without parameter.
A method sayHello() that we can apply to objects of class HungTest.
=> Methods are operations that the class or instances of that class can perform.
A field name. Every Object of HungTest class has an instance of this field.
=> Fields are variables (data) that are local to the class, or instances (object) of that class.
In Class, Object is a instance of a class.
We start a new class and analysis:
- Public keyword is an access specifier.
- Static allows main() to be called without having to instantiate a particular instance of class.
- Void does not return any value.
- main() is the method where java application begins.
- String args[] receives any command line arguments during runtime.
- System is a predefined class that provides access to the system.
- out is output stream connected to console.
- println displays the output
To construct an object, we use the "new" operator which invokes the constructor.
Hung javaTest = new Hung ("Java Test");
=> When an object of a class is created for storing all the data (such as variables and member functions), "new" keyword is used.
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are 3 modifiers:
- Public modifiers that are visible to all classes.
- Private modifiers that are visible only to the class to which they belong.
- Protected modifiers that are visible only to the class to which they belong, and any subclasses.