It depends on what you call it. Java has Packages, which can be mistaken as something similar (in fact it contains also the concept of namespace), after all they put a name in front of the guys. In Java you have:
System.out.println()
C# uses the namespace as a surname for the type, nothing else. It separates into packages called assemblies. For Java the package is yours namespace, so it’s different in many ways.
You can still do it in C#:
using static System.Console;
WriteLine("Olá Mundo");
In Java there is an important semantic difference because in C# we only use the using
as a shortener to not need to use the full name of the type, in Java what is done is a import
of the package to make it available for use, the name has nothing to do with it. Java does some import
automatic s so you can use the most important types.
Note that in a simple "Hello World" in Java is using the class System
and not the package or namespace System
. It’s completely different from C#. See documentation. She is part of the package java.lang
which is automatically imported into every application (must have some configuration to prevent this). In fact the exact mechanism is part of another class.
If you wish not to use the full name you have to do the same as the example I quoted for C# (but it’s still less convenient):
import static java.lang.System.*;
out.println("olá");
I put in the Github for future reference.
You have the same behavior using the word Static after import, ex.:
import static java.lang.System.*;
, you could use it later like this:out.println("Olá")
– Denis Rudnei de Souza
Could you explain a little bit about that statement or run some article on it? The explanation would be why I should use Static after import, why I can’t just use System and what * is there at the end is to select all types of that package?
– Drinjer
Here an article explaining a bit of Static import
– Denis Rudnei de Souza
The reason you need to use out.println is that the println method is not within System, it is a Printstream class method, the out attribute is a Printstream type attribute that is in the System class, so the need to call out.println
– Denis Rudnei de Souza
Ah, I forgot to mention, the * in the end matters everything you have in that package or all attributes of a class when used with Static
– Denis Rudnei de Souza
You might think about using
import static java.io.PrintStream.*;
to use only theprintln()
without having to call theout
before, but will only work if the method isstatic
– Denis Rudnei de Souza