Is there an equivalent of C ++ using namespace for Java?

1

Well, my question is very simple. In C ++ I do this:

using namespace std;

Then I can use the strings. What I would like to know is, if there is something in Java to import all the functions of a class.

Now, what I do is call the functions of the class as if they were properties, like this:

Sistema.pedirTeclado()

But I'd like to use the function without writing Sistema , like this.

pedirTeclado()

Edit:

When I make a folder, and I put the function inside that folder, then it works for me, but when I put all the functions in a class, it does not work for me.

Is there a way to import all the functions of a single file .java ?

My file Sistema.java is this.

package programa;

import java.util.Scanner;

public class Sistema
{
    public static int pedirTeclado(Scanner teclado)
    {
        int número = teclado.nextInt();
        return número;
    }
    public static int pedirTecladoDiferente(Scanner teclado,int número)
    {
        boolean permanece;
        while(permanece)
        {
            int númeroTeclado = teclado.nextInt();
            if(número!=númeroTeclado){permanece = false;}
        }
        return número;
    }
}

I tried to create the functions outside the class Sistema , but it does not leave me either.

    
asked by ArtEze 27.11.2016 в 16:01
source

4 answers

3

You can do a import static of the class .

That makes all static members (methods and attributes) of the imported class can be used as if they were static static members of the class that imports them.

For example, from the class java.util.Math , you can import the value of the PI attribute:

import static java.util.Math.PI;

and then use

double longitud = 2* PI * radio;

If you want to import all the static members of a class, just import * .

import static java.util.Math.*;

Finally, a warning: although legal and correct, this way of working is not used much 1 and can make it difficult to read the code; other people can read the code and think that PI is defined in a superclass or in the same class, etc. Personally, Math.PI has never seemed so long as to justify having to use static imports, and I find that Math.abs () helps to read the code much better.

1 The only case I know of in which it is usually applied is to import the asserts of JUnit.     
answered by 27.11.2016 / 17:19
source
2

import static com.paquete.Sistema.*;

This imports all static methods, that is, those declared with the static modifier. Then they can be used as functions, just as you want to do it, without needing to mention the class:

pedirTeclado(argumentos);

If you want to import a single method, you can do it.

import static com.paquete.Clase.pedirTeclado;

This is the mechanism used by many testing Framworks for functions such as assertEquals .

public class Sistema {

  public static void pedirTeclado() {

  }

  public static void pedirVideo() {

  }

  public static int getHora() {

  }
}
    
answered by 27.11.2016 в 16:22
1

You can use static to no longer invoke the class. It is useful when you need to do something similar to a library for example the class math is a library that you can import it so import java.math if I'm not mistaken, then to access its mathematical functions you only invoke to use them for example sqrt () and you do not use it like this math.sqrt () although in both ways you can, now giving a more useful example you can create a normal class and declare a function so public static add (a, b) and from another class you import and invoke it directly add up (5,6). Note I could not give a better example because I'm from my cell phone then I edit it.

    
answered by 27.11.2016 в 16:59
0

You could do it if you create the method that you want to shorten so that it makes the complete sentence. In your example:

private void pedirTeclado(){
    Sistema.pedirTeclado()
}

If you make what they return, in your case, be the same.

    
answered by 27.11.2016 в 16:23