Do you have anything like the C# namespaces in Java?

Asked

Viewed 302 times

6

I noticed the size of the command to put a message on the screen in Java, wanted to know if it has how to decrease? As in C# that the programmer puts the word using "namespace"; and you can use the "shortened" command".

Ex.: to print the Hello S would be something like:

System.Console.WriteLine("Olá S"); //sem adicionar o using System

Already with the using System would be:

Console.WriteLine("Olá S");

This in C#, how is it in Java? It would have to do something similar for the programmer to put only println("Olá S")?

  • 2

    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á")

  • 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?

  • Here an article explaining a bit of Static import

  • 1

    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

  • 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

  • You might think about using import static java.io.PrintStream.*; to use only the println() without having to call the out before, but will only work if the method is static

Show 1 more comment

1 answer

4


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 importautomatic 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.

  • These Imports can be considered as C headers?

  • This less covenantal has something to do with the paradigm?

  • Are you talking about C/C+? Not exactly, but remember a little, because to such headers in fact are includes, they just stick a text there and good, the import has more semantics, is a more sophisticated mechanism. And the least convenient way is just because Java has never been a big fan of making life easier for the programmer. C# has an identical mechanism, but has an abstraction that makes it more convenient. If I were a Java programmer creating this abstraction and using it on all my systems would be the first thing I would do. Look: https://referencesource.microsoft.com/#mscorlib/system/console.Cs,748bde83a8b838d8

Browser other questions tagged

You are not signed in. Login or sign up in order to post.