NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

A simple java program

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
akromm



Joined: 19 Jan 2010
Posts: 3

PostPosted: Tue Jan 19, 2010 10:20 pm    Post subject: A simple java program Reply with quote

Hey, i use netbeans for c/c++ and its great! But now in school we're learning java. So i want to make a program in java with netbeans.

I'm just having a few issues:

it seems import java.lang.System.out.*; doesnt work. If i want to, for example, print something to the terminal i have to type: java.lang.System.out.println(",,,");

Also i cant seem to figure out why i need: package Assign1; at the top of the .java file? if i remove it, the program doesnt work. (If i make the program in nano for example and use javac it doesnt complain.

I hope that made sense.

Thanks for any help.
Back to top
Humage



Joined: 24 Jul 2009
Posts: 104
Location: Cloud 9

PostPosted: Wed Jan 20, 2010 1:52 pm    Post subject: Reply with quote

File > New Project.. > Java > Java Application

Project Name: JavaYea
Create Main Class: Yes
Set as Main Project: Yes

This results in the most simple java project. It consists of only one file and that has a maximum of around 20 lines of code, including comments.

javayea.Main.java
Code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javayea;

/**
 *
 * @author Humage
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello World");
    }

}


The package declaration tells what 'folder' this file belongs to.
The method, public static void main(String[] args) is what gets ran when you run the program. It's like what starts the application.
As you know C++ I'm sure you know about classes.
Your import statement is not needed as java.lang is used by default.
'out' in java.lang.System.out is a property in the class System.
This property is publicly static, which allows you to access it by using System.out....
When using an import statement, it is only necessary to give the path to a class.

Hope this helps. If not, post a question and I'll try to answer it.

Happy coding, Smile
Back to top
akromm



Joined: 19 Jan 2010
Posts: 3

PostPosted: Wed Jan 20, 2010 9:16 pm    Post subject: Reply with quote

Thanks for the reply.

So is there a way to make a program without needing the "package ...;" at the top? I guess its not too important.

And i tried doing "import System.out.*" so that i could type just "println("...");" instead of System.out.println("..."); but it didn't seem to work. Am i doing it wrong or ...?
Back to top
jussist



Joined: 05 Jan 2010
Posts: 5
Location: Tampere

PostPosted: Thu Jan 21, 2010 11:42 am    Post subject: Reply with quote

akromm wrote:
Thanks for the reply.

So is there a way to make a program without needing the "package ...;" at the top? I guess its not too important.

And i tried doing "import System.out.*" so that i could type just "println("...");" instead of System.out.println("..."); but it didn't seem to work. Am i doing it wrong or ...?


Packages are used to ... package and organize your software. And essential for reusing code. Read http://java.sun.com/docs/books/tutorial/java/package/index.html .

Yes, you are doing it wrong. Import doesn't work that way.
System.out = static field (PrintWriter) in class "System".
println = methodname
System.out.println = call to method "println" of field "out" in class "System".

Just keep studying the book, and answers you will find.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo