Thursday, May 31, 2018

Java Concepts[1]-Classes and Objects

Hi, I’m going to explain important basic java concepts in an easy way using simple and real-life examples.

First I’m going to explain you about java classes and objects.

Class is a user defined blueprint/template that describes the behavior/state the object of its type.

Object is an instance of a class. It consists of states and behavior. 

We’ll take Human class
States ➝ Variables/Fields
Behaviors   ➝    Methods


Following is an example of how to code the above scenario in Java.


#tip 1- Class name should be a noun/nouns and the first letter should be upper case. If there is more than one noun, first letter of each word should be capitalized. (ex. Human, TwoWheelVehicle)

#tip 2- Method name should be a verb and should start with a lower case letter. When there is more than one, the first letter should be lower case with the first letter of internal words capitalized. (Ex-eat, getName)

#tip 3-Variable names should start with a lower case letter. (name, firstName)
           
           There are 3 types of variables.
           1.Local Variables- Variables that declared inside the method. These variables can be used only within that method.

                                                 

            2.Instance Variables-Variable that declared inside a class but outside any method. These variables are initialized when the class is instantiated. These variables can be accessed from inside any method, constructor or blocks of that particular class.



           3.class variables- Variables declared inside a class but outside any method with the static keyword. There is only one copy of this variable that is shared with all instances of that class.

                               


Illustration on instance variables and class variables
'noHumans' is a static variable. So it is stored inside the Class memory and common to all objects. 'name', 'age' and 'weight' are instance variables and they are stored inside the object memory. So each object will have its own copy of instance variables




#tip 4- Every class has a constructor. Constructors should have the same name as the class. A class can have more than one constructor


I hope you understood the classes and objects of Java clearly. If you need more clarification on anything please feel free to leave a comment

Thank You.

Sahani Perera

Wednesday, May 30, 2018

Code Java with NetBeans

Are you a beginner to NetBeans?

 Here is a quick guide for you to start a java project with NetBeans.

1.First Start the NetBeans IDE.

  This is the start page you will get when you start the IDE.

In this it will show the recent projects. As this is your first project, you will get nothing here.



2.Then go to Files  New Project




 3. Then you will get New Project wizard. In that you have to select the category and projects. You need to select them as follows.

Then Click next.


4. Then in this step you need to give the name of your application and the location where you are going to locate it.
             (For my project I gave the name as ‘Example’. You can give a name as your desire and you can browse and select the location you want. )

Project Folder and create main class field will change accordingly. Leave the rest as follows.


Then click Finish.


Then you will get the editor. Beside that you will get the project window and it will contain a tree view of components of the project including source files, libraries etc.

And there is navigator window, which you can use to quickly navigate between elements within the selected class.




Now you are ready to add your code to the project.

Let’s see how to add the code and run the project.

We'll add a simple code to print Hello World.








             Then we need to save, compile and run the project. 

             To save ---> go to file at the top left corner and select save.



No need to do the compilation separately. We only need to click the run button.

  The Green Color Triangle shape button is the run button




Then you will get the output as follows.



If you get the output as above,  Congratulation your code is working.

Now it is time for you to add more codes and run the program.