Wednesday, February 20, 2019

Lesson 4 - Object

Create a new class, name forth.
Copy in to class bracket.
public static void main(String args[])
{
     third obj = new third();
     obj.show();
}
Run to see result.

Copy to continue.
int so=15;
int so2=4;
obj.multiply(so,so2);
Run to see result.

We ‘ve just create and use object.
The line third obj = new third(); create object obj of class third
Connect object with method in class by dot, we can access any method in class.
When we make app, if need to use a method in many class, we don’t need to copy method to all class, just create an object of holder class and we can use any method inside it.
Back to class third, change te word public of show() to private, press Save. You will see in class forth an error.
Move cursor to it, Eclipse say it can’t find show() method. That because the keyword private make show() can only use inside class third. Delete the word private, error will go.
The words public, private, protected are access scope of method. When start to learn, you should use all public and no need to care about them.
If you delete it, Eclipse make it defaul public.

Try to access other method in class third.
Now you can understand what is a class, it open with Public class ClassName{ end with }

That ‘s very simple and easy. Don’t just read definitions in programme books, let code something and you will understand all concepts.

No comments:

Post a Comment