|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
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 java Code:
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. Code:
package helloworld;
public class MyFirstApp {
public MyFirstApp(){
}
}
Code:
package helloworld;
public class MyFirstApp {
public MyFirstApp(){
System.out.println("Hello World!");
}
}
In our example we will be using an applicationCode:
package helloworld;
public class MyFirstApp {
public MyFirstApp(){
System.out.println("Hello World!");
}
public static void main(String[] args){
new MyFirstApp();
}
}
VIDEO TUTORIAL: http://www.extreme-hq.com/other/Vide...loWorldOne.wmv 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: Code:
package helloworld;
public class MySecondApp {
public MySecondApp(){
}
public static void main(String[] args) {
new MySecondApp();
}
}
Code:
package helloworld;
import javax.swing.*;
public class MySecondApp {
public MySecondApp(){
}
public static void main(String[] args) {
new MySecondApp();
}
}
Java Code:
5. Finally we are going to create a window using the the JFrame methods. I will discuses these more in depth in later tutorials. Java Code:
VIDEO TUTORIAL: http://www.extreme-hq.com/other/Vide...loWorldTwo.wmv And your done ![]() Last edited by John; 08-29-2007 at 02:51 PM. |
| Sponsored Links |
|
|
|
|||
|
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 11:32 AM. |
|
|||
|
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. 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 10:50 PM. |
| Sponsored Links |
|
|
|
|||
|
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 04: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. ![]() |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java:Tutorial - Making multiple objects work differently | John | Java Tutorials | 0 | 01-11-2007 02:10 PM |
| Java:Tutorial - A better looking GUI | John | Java Tutorials | 0 | 01-11-2007 02:07 PM |
| Java:Tutorial - Make Your Button Work | John | Java Tutorials | 0 | 01-11-2007 02:04 PM |
| Java:Tutorial - Adding Buttons to your Interface | John | Java Tutorials | 0 | 01-11-2007 02:03 PM |
| Tutorial: PHP "Hello World" | Void | PHP Tutorials | 4 | 08-27-2006 11:35 AM |