Alex
Alex

Reputation: 1228

Make a group of variables public in java

Is there an easy way to make a group of variables in a class as public. For example to make a group of variables as static, I can use something like below.

class A {
    static {
        int x;
        int y;
    }
}

Is there a way to do something similar to make the variables as public. Something like this.

public {
        int x;
        int y;
        }

Edit:

I understood that the static block doesn't do what I supposed it will do. What I need is a java version of something like this in C++

 class A {
      public:
          int x;
          int y;
  }

Upvotes: 0

Views: 2038

Answers (4)

Ubica
Ubica

Reputation: 1051

static int x,y,z;

This should make a group of variables static... as long as they are of the same type of course. You can also make them all public, private etc.

public static int x,y,z;

Upvotes: 0

Everv0id
Everv0id

Reputation: 1982

You could use public int x,y. And be careful about static {} blocks.

Upvotes: 0

nitishagar
nitishagar

Reputation: 9413

The above code declares a static initialization block which is not same as declaring the variables as static. (More info.)

For the problem at hand you have to declare variables public separately.

Upvotes: 0

Eran
Eran

Reputation: 393841

Your code sample doesn't make a group of variables static, it declares local variables in a static initialization block.

You'll have to declare each variable as public separately.

Upvotes: 5

Related Questions