Object:
To build your very first Java App
The Idea:
Like every first tutorial, this tutorial will show you how to display "Hello World," but in two ways. The first most simple way, will print "Hello World" to the console, the second (although I wont fully explain everything until later tutorials) will display "Hello World" using the OOP aspect of Java.
Prerequisites:
No Previous knowledge of Java is needed but you should have read this tutorial:
http://forum.codecall.net/tutorials-...g-started.html
The Tutorial:
1. Create a new class called MyFirstApp
[highlight=java]package helloworld;
public class MyFirstApp {
}
[/highlight]
2. Above is the basic structure of a class. The first line is the package declaration. It serves as a "folder" that holds a bunch of class's. The second line is the class declaration header. The next step is to create a constructor.
3. The third line is the constructor declaration. It is started with an access modifier public/private and then followed by the name of the class with an closed set of parentheses. At this point we don't have much so lets get to the good stuff!Code:package helloworld; public class MyFirstApp { public MyFirstApp(){ } }
4. At this point we have a fully functioning class. However we need to create a means of starting it. There are two ways, you could implement an applet or make it an application. At this point, lets not get hung up on the differences or the what the syntax means just know it works.Code:package helloworld; public class MyFirstApp { public MyFirstApp(){ System.out.println("Hello World!"); } }In our example we will be using an application
5. public static void main(String[] args){ is the method java looks for to initialize the application. The "new" creates a new instance of the class in the computers memory and viola your code works.Code:package helloworld; public class MyFirstApp { public MyFirstApp(){ System.out.println("Hello World!"); } public static void main(String[] args){ new MyFirstApp(); } }
VIDEO TUTORIAL: Temp
However, printing words to the console can get pretty boring, to show you some of Javas capabilities, I will give you a brief introduction to working with swing. Although I wont explain in depth the reason behind each line of code as that is beyond the scope of this tutorial.
1. Create a class as shown above, but this time name it MySecondApp
2. Create a constructor and instantiate it in the main method. At this point we should have something like this:
3. Next we are going to import the swing package that is avalable to usCode:package helloworld; public class MySecondApp { public MySecondApp(){ } public static void main(String[] args) { new MySecondApp(); } }
4. Next we are going to extend JFrame (which is a class in the swing package) so we can inherit its capabilities.Code:package helloworld; import javax.swing.*; public class MySecondApp { public MySecondApp(){ } public static void main(String[] args) { new MySecondApp(); } }
[highlight=Java]package helloworld;
import javax.swing.*;
public class MySecondApp extends JFrame {
public MySecondApp(){
}
public static void main(String[] args) {
new MySecondApp();
}
}[/highlight]
5. Finally we are going to create a window using the the JFrame methods. I will discuses these more in depth in later tutorials.
[highlight=Java]package helloworld;
import javax.swing.*;
public class MySecondApp extends JFrame {
public MySecondApp(){
setSize(150,60);
setLocation(20,20);
JLabel myLabel = new JLabel("Hello World!");
add(myLabel);
setVisible(true);
}
public static void main(String[] args) {
new MySecondApp();
}
}[/highlight]
VIDEO TUTORIAL: Temp
And your done
Originally posted as Java – Hello World
Last edited by John; 08-01-2010 at 10:11 AM.
Pretty Simple Step by Step guidance. Thanks for the best efforts, Which helped me a lot. Better if you could include the way of compiling and running the program.
Last edited by AfTriX; 01-18-2007 at 09:32 AM.
My tutorials assume you are using Eclipse which allow you to compile and run the app within the IDE
If I am writing the codings in Notepad and Running through Command Prompt then what are the procedures that I got to follow.
I am not aware of saving the file properly
- File Name
- How to Compile
- How to run
I get the following errors when I try to run.
MyFirstApp is compiled perfectly, but not aware of running the prog.Code:javac helloworld.java helloworld.java:3: class MyFirstApp is public, should be declared in a file name d MyFirstApp.java public class MyFirstApp { ^ 1 error
I'm starting Programing Java through your Tutorials. Help me go on with Java.
Last edited by AfTriX; 01-18-2007 at 08:50 PM.
Well you should then know that javac is the command to compile the java file. Therefore MyFirstApp is not compiled perfectly. The file should be named MyFirstApp.java not helloworld.java. Once you have the file renamed, compile it usingwhich should create another file called MyFirstApp.class which is your java code converted into machine language. To run your application you should use the commandjavac MyFirstApp.javaof course those commands assume you have the java directory defined in your system variablesjava MyFirstApp
Last edited by John; 01-18-2007 at 11:33 PM.
Once again Thanks a lot for your guidance, I'll try this way and come back to you. I'm keeping on improving my skills on Java. Hope to have a great Programmer "SideWinder" as my guide.
I have the hello world program from the Sun Java tutorial and it is as follows:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
the code from this tutorial is:
package helloworld;
public class MyFirstApp {
public MyFirstApp(){
System.out.println("Hello World!");
}
public static void main(String[]args){
new MyFirstApp();
}
}
Both do the same, why?
My tutorial prints the code in the constructor and uses the main method to instantiate the class. Once the class is instantiated, the constructor is the first code to run.
Essentially, in my code, the main method runs, which then calls the constructor which then prints to the console.
Their tutorial just prints to the console as soon as the main method is invoked rather than calling the constructor.
Last edited by John; 07-17-2007 at 02:16 PM.
I am very new to this, just started in fact. As I continue will I learn about constructors, instantiating, calling, and mains and stuff. Sorry if I sound like a dolt, just trying to understand this and don't know if there is a more basic level I should start on, or if I will learn this stuff as I go.
The constructor is a "method" with the same name as the class.
If you instantiate something, you create a new object, which you can do "things" to.
public static void main(String[] args) is your main method. The first method that is ran when you run any java application.
If you instantiate the class in the main method by using the new keyword, you create an object of the class, and the constructor is the first method that is ran after the main method.
![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks