Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the main class in a java source file?

Tags:

java

im new to java.

the standard code that is created after i created a new project in netbeans is:

package helloworldapp;

public class Main {

    public static void main(String[] args) {
        int[] array = new int[10];
        array[9] = 1;

        System.out.println(array[9]);
    }
}

so you see that it uses the System class, but i dont see that class has been imported. Is there another code somewhere that does that? How can i use it if its not imported.

like image 292
ajsie Avatar asked Jul 06 '26 13:07

ajsie


2 Answers

The System class is, along with everything else in java.lang, imported automatically. Other classes you probably use that are automagically imported include String and Exception.

like image 185
Stu Thompson Avatar answered Jul 08 '26 02:07

Stu Thompson


Additional info:

How can i use it if its not imported.

you can use any class in another package, without importing it, by using the fully qualified name. Example:

    java.util.Date now = new java.util.Date();

instead of

import java.util.Date;  // or java.util.*;
...
    Date now = new Date();

Update:
import is only used at compile time to determine the fully qualified name of classes.

like image 25
user85421 Avatar answered Jul 08 '26 01:07

user85421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!