java package vs System in JAVA - 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).

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.

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();

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.

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();

Related

JavaFX - access class in upper package - Can't resolve symbol [duplicate]

This question already has answers here:
How to access java-classes in the default-package?
(5 answers)
Closed 5 years ago.
Is it possible to import a class in Java which is in the default package? If so, what is the syntax? For example, if you have
package foo.bar;
public class SomeClass {
// ...
in one file, you can write
package baz.fonz;
import foo.bar.SomeClass;
public class AnotherClass {
SomeClass sc = new SomeClass();
// ...
in another file. But what if SomeClass.java does not contain a package declaration? How would you refer to SomeClass in AnotherClass?
You can't import classes from the default package. You should avoid using the default package except for very small example programs.
From the Java language specification:
It is a compile
time error to import a type from the
unnamed package.
The only way to access classes in the default package is from another class in the default package. In that case, don't bother to import it, just refer to it directly.
That's not possible.
The alternative is using reflection:
Class.forName("SomeClass").getMethod("someMethod").invoke(null);
As others have said, this is bad practice, but if you don't have a choice because you need to integrate with a third-party library that uses the default package, then you could create your own class in the default package and access the other class that way. Classes in the default package basically share a single namespace, so you can access the other class even if it resides in a separate JAR file. Just make sure the JAR file is in the classpath.
This trick doesn't work if your class is not in the default package.
It is not a compilation error at all! You can import a default package to a default package class only.
If you do so for another package, then it shall be a compilation error.

Creating an object of a class from another class using NetBeans in Java [duplicate]

This question already has answers here:
How to access java-classes in the default-package?
(5 answers)
Closed 5 years ago.
Is it possible to import a class in Java which is in the default package? If so, what is the syntax? For example, if you have
package foo.bar;
public class SomeClass {
// ...
in one file, you can write
package baz.fonz;
import foo.bar.SomeClass;
public class AnotherClass {
SomeClass sc = new SomeClass();
// ...
in another file. But what if SomeClass.java does not contain a package declaration? How would you refer to SomeClass in AnotherClass?
You can't import classes from the default package. You should avoid using the default package except for very small example programs.
From the Java language specification:
It is a compile
time error to import a type from the
unnamed package.
The only way to access classes in the default package is from another class in the default package. In that case, don't bother to import it, just refer to it directly.
That's not possible.
The alternative is using reflection:
Class.forName("SomeClass").getMethod("someMethod").invoke(null);
As others have said, this is bad practice, but if you don't have a choice because you need to integrate with a third-party library that uses the default package, then you could create your own class in the default package and access the other class that way. Classes in the default package basically share a single namespace, so you can access the other class even if it resides in a separate JAR file. Just make sure the JAR file is in the classpath.
This trick doesn't work if your class is not in the default package.
It is not a compilation error at all! You can import a default package to a default package class only.
If you do so for another package, then it shall be a compilation error.

Why has object been created without importing class? [duplicate]

This question already has answers here:
Java: Use import or explicit package / class name?
(3 answers)
Closed 8 years ago.
I came across some java code where the following statement was present.
com.myproject.bar.Foo foo = new com.myproject.bar.Foo();
The class com.myproject.bar.Foo has not been imported into the class but an object of Foo is created in the class where this statement is written.
Why has such an implementation been done? Are there any advantages of using such an implementation over doing an import of the class Foo?
It's instantiation with the fully-qualified name of the class.
com.myproject.bar.Foo foo = new com.myproject.bar.Foo();
This doesn't require to add an import statement, because you've already told the compiler which is the package of the class you want to instantiate.
Sometimes this is used when there are several classes with one and the same simple name.
If you'd like to do this:
Foo foo = new Foo();
then you will have to import the class:
import com.myproject.bar.Foo;
An import statement just makes the type available by its short name without specifying the package. That's all it does. It's not like the class can't be used without an import.
Usually it's clearer to use an import instead, but sometimes that's not possible - you may want to use two classes both called Foo from different packages with the same class, for example. (This is most common when you've got two representations of the same entity - an API representation and a storage representation, for example.)
Without knowing what the real code looks like, we can't tell whether that was the case here, or whether an import would have been fine. If it can work, an import is usually more readable.
If you address the class with fully qualified name such as com.myproject.bar.Foo, you need to specify it every time when you try to access that class.
But in case of import, you dont need to.
That's one of the advantages.
com.myproject.bar.Foo is absolute class name.
Foo is just the Class name(relative) and it can be in any package, so to specify the package, import statements are used.
In cases where you have more than one class with same name, you have to use the absolute class name in your code to distinguish between the duplicate names. Example com.Foo and org.Foo then you use Foo for com.Foo and org.Foo for org.Foo.
There is no special advantages.
But useful in case if two classes have same class name and belongs to different packages then you need to specify the fully qualified package name to differentiate both while using them in a Class.
Consider 2 classes
com.myproject.bar.Foo
com.myproject.bar.innerbar.Foo
Now in some other class you are going to instatiate like
Foo foo = new Foo();
Which Foo to import now?
To avoid that ambiguity, we need to specify the full package name.

How do I avoid unnecessary class references in Java?

I have two java classes (.java files). I want to be able to create new instances of an object in one class that were defined in the other class, without referencing the class name each time. In C# there are #using and includes, but I am only able to use import <pre-compiled code> in java. Is there a way to do this:
import TestClass;
and then simply call a function inside it without using
TestClass.TestFunction()
every time? I simply need to have all of my functions and objects to be separate from my Main class.
Assuming TestFunction is a static method in TestClass, you can use a static import:
import static TestClass.TestFunction;
// or
import static TestClass.*;
and then call it without using the class qualifier:
TestFunction(...);
Note this can lead to confusing/hard-to-read code – use static imports sparingly.
If you're using netbeans then you won't be finding any issue during the import of any java classes. And yeah that's true you can have only "pre-compiled classes" as import statement. If you are running on notepad then you need to compile your independent classes first and then your dependent classes. And if you use Netbeans or Eclipse or any other IDE then you do not have to worry they will manage by themselves, you just have to use proper package and class names
You can have two types of imports
import package1.Class1;
import static package1.Class2;
With the first one you can create object of Class1 (or any other class if present in Class1) and invoke the methods
With the second one you can directly call the static methods of Class2 without referencing it with classname
Updated
See tutorials on packages in JAVA

Java: Use import or explicit package / class name?

I'm writing a very basic app, extending the Swing JFrame. What are the differences between making an explicit reference:
public class LineTest extends javax.swing.JFrame {
...
or importing the class beforehand:
import javax.swing.JFrame;
public class LineTest extends JFrame {
...
When (and why) would you use one technique over the other?
There is no real difference; the generated byte code will be exactly the same. An import statement is really just a way to tell the compiler "when I write JFrame, I actually mean javax.swing.JFrame".
You might want to use fully-qualified package names if you have for example two classes with the same name in different packages. One common example from the standard library are the classes java.util.Date and java.sql.Date.
The only difference is in the source code. Using the fully qualified name leads to less readable code, so everyone uses imports pretty much exclusively. The only place where I've ever seen the fully qualified names used consistently is in generated code.
The only reason to use the fully qualified name in regular code is when you have to use two classes with the same simple name (e.g. java.util.List and java.awt.List) - in that case there is no alternative.
For the compiler it doesn't make any difference. The disadvantage of using the full qualified name is that you would have to write it each time you are using the class. I only use the full qualified name if I have two classes with the same name in different packages. This way you can differ between those two classes

Categories