Jump to content

  •  

Photo
- - - - -

Lesson 1: Creating a Class

Java for Beginners

  • Please log in to reply
No replies to this topic

#1 Wilson18

Wilson18

    Administrator

  • root
  • 112 posts
  • LocationUniversity of Birmingham

Posted 08 October 2014 - 02:28 AM

In this very basic tutorial, I will be showing you how you can create a new class main class within java. The first step is to create a file with the name of the class you would like to create. This must start with a capital letter and have the file extension of .java. My file will be called MyFirstClass.java

 

In this file, we start with a line defining the class and the main method.

public class MyFirstClass {
	public static void main(String[] args){
	
	}
}

It is important to note that whenever you create a method (function) you start and finish it with a { or } and that all code goes within that. This is the same for classes. 

 

Once you have entered all of this, we can use a built in function to print a string out. to do this, we type the following inside our main method. 

System.out.println("Hello World");

Another very important thing to note is that you have to be very careful with capital letters and only use them where shown. If you enter them elsewhere then it may not compile and not work. You also need to make sure that you finish every command with a semi-colon  ";".

 

If you have entered it all correctly, it should look something like this: 

public class MyFirstClass {
	public static void main(String[] args){
		System.out.println("Hello World");
	}
}

Thats all for this tutorial. Check out the next one to find out how to compile and run your java program we have just created.

Attached Files


List what you read?

Check me out on: YouTube, Twitter, Facebook, Google+






Also tagged with one or more of these keywords: Java for Beginners