Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java:Tutorial How to understand a code and using libraries.

source codes class timer

  • Please log in to reply
2 replies to this topic

#1 madushan1000

madushan1000

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Location:Sri Lanka
  • Programming Language:Java, Python
  • Learning:Java, C#, PHP, Python, JavaScript, NXT-G

Posted 10 September 2012 - 12:24 AM

Before start coding,We should able to understand already written codes.Lets get to that.I'm going to use Netbeans IDE for the rest of my tutorials.I told how to set it up in this tutorial(http://forum.codecal...what-is-an-ide/).

Let's consider this simple code,

package helloworld;
public class Main
{
	public static void main(String[] args)
	{
		hellorun("Hello World");
	}
	public static  void hellorun(String input)
	{
		hello he=new hello();
		he.run(input);
	}

}
class hello
{
	public void run(String input)
	{
		System.out.println(input);
	}
}

Let's take a look at line by line,

package helloworld;

What is a package?,
A package is a container of classes.Take a look at the code,There 2 classes,They both belongs to package helloworld.

Then comes the classes,
A class is a container of methods.

Well the methods are a containers of instructions to do a task.

Let's see what have I done in this code,When code runs It just prints out "Hello World".

class hello
{
	public void run(String input)
	{
		System.out.println(input);
	}
}

class hello,Here I defined the class.

public void run(String input),definition of the method named "run".
Did you noticed the string variable "input" in brackets? That means we will pass a "String" as a parameter.This is a way of give some input to a method.I'll explain more in further tutorials.

Then comes the same old print statement.This will print the content of variable "input".

public class Main
{
	public static void main(String[] args)
	{
		hellorun("Hello World");
	}
	public static  void hellorun(String input)
	{
		hello he=new hello();
		he.run(input);
	}
}

This is the main class,Not because of it's name is "main".You can have any name for a class(There are some exceptions!).But because it contain the "main" method.

public static void hellorun,This method creates a object of class "hello" and then calls the "run" method in it,passing "input" as the parameter.


The main method public static void main,is calling the method hellorun passing "Hello World" as the parameter.

main.PNG

Ok,That might give you and idea of how to understand a code.
Let's take a look at few popular java packages.


As we can define java packages,Java comes with quite a lot of inbuilt packages that could make our work easy 1000 times as usual.

java.lang - Most usable java package.Handle most of the language.
java.util - Contents most of the utility classes like scanner,timer and so on.
javax.swing - Javas default GUI tool kit.Could be used to built simple GUI application to most advanced and complex GUI applications
java.io - Handles input and output actions

There another hundreds of packages and these are the important ones.

So how to use one of these in out code?.
That's when the "import" keyword comes in,

Check out this code,

import javax.swing.*;
public class test
{
	public static void main(String[] args)
	{
		  JOptionPane.showMessageDialog(null, "hello");
	}
}

Noticed the line "import javax.swing.*" ?
That means import everything in javax.swing package into out program.we can import any amount of packages in to our program.

Here we have another problem,Can we use the packages built by 3rd party?.
Of cause we can.Before we can use them,we have to ad them to our libraries.To do this in net beans

Click on the "window" on the menu bar and then "projects" or else "ctrl+1".
Then on the "projects" tab expand our currant project by clicking on it right click on "Libraries" and then click "Add jar/folder" navigate to the jar file or folder you want to ad as a library to the program and click "open"

lib.png

Then Import the classes you want from the library to program.Here is the sample netbeans project files for today's tutorial and happy coding :D

Attached File  helloworld.zip   14.38KB   252 downloads

Take a look at my other tutorials too
http://forum.codecal...what-is-an-ide/
http://forum.codecal...-hello-to-java/
  • 2

#2 maher

maher

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Location:India
  • Programming Language:C, C++, (Visual) Basic
  • Learning:Java, PHP, JavaScript

Posted 05 October 2012 - 06:31 PM

thanks madushan1000 for the valuable information
  • 0

#3 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 05 October 2012 - 10:49 PM

Very nice ... thanks +1
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.






Also tagged with one or more of these keywords: source codes, class, timer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download