final class, global?/

B

bigbinc

silly question, can final classes be used like global classes that can
be accessed by other classes. I have done it, but It feels like I am
following bad programming practice.

I.E.
public final class x {

static int y;

x() { y = 3; }

public static void addY() {
y++;
}

} // end of class


and then if I call this in other functions

x.addY();

x.addY();

it does change y? is this worth doing?
 
V

VisionSet

bigbinc said:
silly question, can final classes be used like global classes that can
be accessed by other classes. I have done it, but It feels like I am
following bad programming practice.

I.E.
public final class x {

static int y;

x() { y = 3; }

public static void addY() {
y++;
}

} // end of class


and then if I call this in other functions

x.addY();

x.addY();

it does change y? is this worth doing?

All a final class is, is one that cannot be subclassed.
Usually to act as a constraint on client programmers of your package.
It is not like a class scoped final as in a final attribute.
 
A

Adam Maass

bigbinc said:
silly question, can final classes be used like global classes that can
be accessed by other classes. I have done it, but It feels like I am
following bad programming practice.

I.E.
public final class x {

static int y;

x() { y = 3; }

public static void addY() {
y++;
}

} // end of class


and then if I call this in other functions

x.addY();

x.addY();

it does change y? is this worth doing?


"final" on a class means only that it can't be subclassed.

final class X ...

class Y extends X // compile-time error


I don't know what you mean by a "global" class. A public class is visible
and accessible to every other class in the system.


In your example, you have a class (called x) with a constructor, a static
variable and a static method. The constructor is used to make new instances
of the class. (Along with the "new" keyword.) "static" means that the
variable and the method are shared among all instances of the class. In
particular, for static methods, you do not need an instance to call the
method; all you do is put the class name instead.

Your lines:

x.addY();
x.addY();

do not operate on any particular instance; they call the static method
called addY in the class x. After these lines complete (assuming that they
are the first things that run after class x is loaded and initialized) the
variable y should hold the value 2.


I don't know what you're trying to accomplish, but this code is unlikely to
do anything useful.

In particular, as soon as you construct a new x:

new x();

The value of y goes to 3. (Because this is exactly what the constructor
says.)

Let's follow this up:

new x(); // y == 3
x.addY(); // y == 4
x.addY(); // y == 5
new x(); // y == 3


Is this what you intended?


-- Adam Maass
 
B

bigbinc

Adam Maass said:
"final" on a class means only that it can't be subclassed.

final class X ...

class Y extends X // compile-time error


I don't know what you mean by a "global" class. A public class is visible
and accessible to every other class in the system.


In your example, you have a class (called x) with a constructor, a static
variable and a static method. The constructor is used to make new instances
of the class. (Along with the "new" keyword.) "static" means that the
variable and the method are shared among all instances of the class. In
particular, for static methods, you do not need an instance to call the
method; all you do is put the class name instead.

Your lines:

x.addY();
x.addY();

do not operate on any particular instance; they call the static method
called addY in the class x. After these lines complete (assuming that they
are the first things that run after class x is loaded and initialized) the
variable y should hold the value 2.


I don't know what you're trying to accomplish, but this code is unlikely to
do anything useful.

In particular, as soon as you construct a new x:

new x();

The value of y goes to 3. (Because this is exactly what the constructor
says.)

Let's follow this up:

new x(); // y == 3
x.addY(); // y == 4
x.addY(); // y == 5
new x(); // y == 3


Is this what you intended?


-- Adam Maass

Ok, so the same thing can be done with static variables, got ya., duh.



public class XYZ {

static int theVal = 0;

public static int goIncrement() {

System.out.println("" + theVal);
theVal++;

return theVal;

} // end of the fnuc


} // endof the class


public class theMain {

public static void main(String [] args) {

System.out.println("" + XYZ.goIncrement());

System.out.println("" + XYZ.goIncrement());
System.out.println("" + XYZ.goIncrement());
System.out.println("" + XYZ.goIncrement());

} // end of th efunctino

} // end of class
 
B

bigbinc

Adam Maass said:
"final" on a class means only that it can't be subclassed.

final class X ...

class Y extends X // compile-time error


I don't know what you mean by a "global" class. A public class is visible
and accessible to every other class in the system.


In your example, you have a class (called x) with a constructor, a static
variable and a static method. The constructor is used to make new instances
of the class. (Along with the "new" keyword.) "static" means that the
variable and the method are shared among all instances of the class. In
particular, for static methods, you do not need an instance to call the
method; all you do is put the class name instead.

Your lines:

x.addY();
x.addY();

do not operate on any particular instance; they call the static method
called addY in the class x. After these lines complete (assuming that they
are the first things that run after class x is loaded and initialized) the
variable y should hold the value 2.


I don't know what you're trying to accomplish, but this code is unlikely to
do anything useful.

In particular, as soon as you construct a new x:

new x();

The value of y goes to 3. (Because this is exactly what the constructor
says.)

Let's follow this up:

new x(); // y == 3
x.addY(); // y == 4
x.addY(); // y == 5
new x(); // y == 3


Is this what you intended?


-- Adam Maass


If you dont call the 'new' what happens? Is it just instantiated
on the first use?

theStaticClass_x.addY(); // y == 4
theStaticClass_x.addY(); // y == 4
theStaticClass_x.addY(); // y == 5
 
B

bigbinc

Adam Maass said:
"final" on a class means only that it can't be subclassed.

final class X ...

class Y extends X // compile-time error


I don't know what you mean by a "global" class. A public class is visible
and accessible to every other class in the system.


In your example, you have a class (called x) with a constructor, a static
variable and a static method. The constructor is used to make new instances
of the class. (Along with the "new" keyword.) "static" means that the
variable and the method are shared among all instances of the class. In
particular, for static methods, you do not need an instance to call the
method; all you do is put the class name instead.

Your lines:

x.addY();
x.addY();

do not operate on any particular instance; they call the static method
called addY in the class x. After these lines complete (assuming that they
are the first things that run after class x is loaded and initialized) the
variable y should hold the value 2.


I don't know what you're trying to accomplish, but this code is unlikely to
do anything useful.

In particular, as soon as you construct a new x:

new x();

The value of y goes to 3. (Because this is exactly what the constructor
says.)

Let's follow this up:

new x(); // y == 3
x.addY(); // y == 4
x.addY(); // y == 5
new x(); // y == 3


Is this what you intended?


-- Adam Maass


One more question, and I use google which is slow. And this question
is more related to how java works. Is it true you can only use static
members through static functions. It almost seems like that particular
class is split in half. One half can only access static members, and
the other half can only access non-static, interesting...
 
P

pete kirkham

bigbinc said:
One more question, and I use google which is slow. And this question
is more related to how java works. Is it true you can only use static
members through static functions. It almost seems like that particular
class is split in half. One half can only access static members, and
the other half can only access non-static, interesting...

An instance method of the class can access all static members of the
class and non-static members of the instance referenced by the variable
'this'.

A static method of the class can access all static members of the class
and non-static members of any arbitary instance of the class.

The members you may access via 'this' in
class Foo {
void foo () {
this.something;
}
}

are identical to the members you may access via the 'self' in
class Foo {
static void foo (Foo self) {
if (self!=null) {
self.something;
}
}
}


Pete
 
S

Sandip Chitale

The feature you are exploiting is not due to 'final' ness of the
class but the 'static' ness of the methods.
 
A

Adam Maass

bigbinc said:
One more question, and I use google which is slow. And this question
is more related to how java works. Is it true you can only use static
members through static functions. It almost seems like that particular
class is split in half. One half can only access static members, and
the other half can only access non-static, interesting...

No, not quite. Static variables and methods can be used in /any/ method of
the class. But you can't use non-static members in static methods.

-- Adam Maass
 
R

Roedy Green

public final class x {

static int y;

x() { y = 3; }

public static void addY() {
y++;
}

} // end of class

Your code had me baffled until I realised that x was your constructor,
not a mangled method definition. Please use caps for class names and
lower case for variables and methods.


The final in that context means the class is final, not that a
reference to the class is final. You can't subclass X, but you can
change references all you want to X objects (unless they too are
marked final).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top