Nick Birke
Nick Birke

Reputation: 47

java package vs System in JAVA

I am just learning JAVA (again in a very long time) and I have a simple question. What is the difference between java.something and System.something?

The beginner book I got is not thorough enough to explain this (at least not yet).

Upvotes: 1

Views: 487

Answers (4)

marekmuratow
marekmuratow

Reputation: 404

System class belongs to java.lang package and all classes in the java.lang package are imported by default so you do not need to import java.lang.*;

On the other hand to use class from java.something package you must write package name with class name

Object obj = new java.something.YourClass();

or you must use import statement

import java.something.YourClass;
...
Object obj = new YourClass();

Upvotes: 0

Jesper
Jesper

Reputation: 206816

In Java, classes and interfaces are organized in packages (<- click to go to the tutorial).

Class System is one of the classes in the package java.lang.

If you see for example System.out, it means you are doing something with the member variable out which is part of class System.

When you see for example java.util.Date, then it means you are using the class Date which is in the package java.util. You can either use the fully qualified name of the class, which is java.util.Date, or you can import the class and then just use the class name Date:

// At the beginning of your source file
import java.util.Date;

// Now you can just use the short name Date instead
// of the long name java.util.Date
Date now = new Date();

Upvotes: 1

duffymo
duffymo

Reputation: 308763

Hard to tell, but System.something is really shorthand for java.lang.System.something. You're referring to the System class in the JDK.

If you see java.something, it's going to be a package name built into the JDK (e.g. java.sql, java.util, etc.) That's reserved by Sun/Oracle for JDK packages.

Upvotes: 0

chiastic-security
chiastic-security

Reputation: 20520

In the case of java.something, this is a package: a package contains various classes that are available for use in your program. If you want to use a particular class--say, java.util.Random--you can either refer to it in full as java.util.Random, or have an import line at the top of your class that tells Java where to find the Random class, and then refer to it just as Random.

System is a class, and it's contained in the java.lang package. (And java.lang classes are always imported into your project, so you don't need the import line in this case.) When you refer to System.something(), you're referring to the System class, and invoking the something() method of that class.

Most of the time, if you want to invoke method something() on class Someclass, then you create an instance of Someclass and then call something() on it:

Someclass x = new Someclass();
x.something();

but for a static method, you invoke it without needing to create an instance. The methods of System are static, so you just invoke them as

System.something();

without creating an instance of type System.

Upvotes: 1

Related Questions