1

It is possible to import just one static method, or member in extend? I know that it is possible to import all static methods by *, like in this example ...

import static example.Types.*

But I need only one.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60

2 Answers2

0

If what you're looking for is something like the using keyword from C# or C++ java doesn't have that feature.

Java 5 added an import static option that allows static variables (typically constants) to be referenced without qualifying them with a class name.

For example, after

import static java.awt.Color;

It would then be possible to write

Color background = RED;

instead of

Color background = Color.RED;

Example above have been taken from: Java: packages and Import. The site used for the example seems to be out of date, sorry any trouble this can cause

See How java import works for more detail.

Community
  • 1
  • 1
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
0

Yes it is possible.

import static System.out;

You can then merely write

out.println("Doobedoo");

Which will save you from typing the System.out part several times over but is potntially confusing for anyone trying to follow you code.

Use it sparingly and generally only for constants ( static final ).

Chaffers
  • 176
  • 9