Brandon
Brandon

Reputation: 1195

Why doesn't this default java import work?

I'm learning java and I'm told this package is provided by default, to every class, because its methods are so common. I thought I would try to import it, any way to see what would happen. I know its not practical and probably expensive but I'm curious as to why it's doesn't work from a technical point of view.

import javax.lang.*;//why doesn't this work.

Upvotes: 0

Views: 111

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53565

javax.lang contains only a single package: model https://docs.oracle.com/javase/7/docs/api/index.html?javax/lang/model/package-summary.html

you're not doing anything by importing this package. Maybe you're confusing it with java.lang ?

Upvotes: 1

eBourgess
eBourgess

Reputation: 303

You don't need to import java.lang.*

There is one exception to the import rule. All classes in the java.lang package are imported by default. Thus you do not need to import java.lang.*; to use them without fully qualified names.

Consider the System.out.println() method we've been using since the first day of class.

System is really the java.lang.System class. This class has a public static field called out which is an instance of the java.io.PrintStream class. So when you write System.out.println(), you're really calling the println() method of the out field of the java.lang.System class.

Upvotes: 0

Related Questions