Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Good idea or full of it?

1 view
Skip to first unread message

Abrasive Sponge

unread,
Sep 23, 2004, 3:48:59 PM9/23/04
to
public class Person {
private String firstName;
private static int count;


public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {
return firstName;
}

public static setCount(int count) {
static.count = count; //using a static keyword as such
}

public static int getCount() {
return static.count; //using a static keyword as such
}
}

I was wondering if the java language can use something
like this....maybe there is something that I don't know, but having a
static reference like this would kick butt.

John Davison

unread,
Sep 23, 2004, 4:26:43 PM9/23/04
to
Abrasive Sponge wrote:

Do you mean this?

public static setCount(int count) {
Person.count = count;
}

public static int getCount() {
return Person.count;
}

Now, go kick some butt! ;)

- john

Abrasive Sponge

unread,
Sep 23, 2004, 5:03:19 PM9/23/04
to
John Davison wrote:


Hahaha, I do that already :)

It's just a feature unused that makes sense, so I thought why not. :)

Bryce

unread,
Sep 23, 2004, 5:06:13 PM9/23/04
to

uhhh... Would this do?

public class Person {
private String firstName;
private static int count;

public static void setCount(int count) {
Person.count = count;
}

public static int getCount() {
return count;
}

public static void main(String[] args) {
Person.setCount(100);

System.out.println("getting count [" +
Person.getCount() + "]");
}
}

or am I missing something in your request...

--
now with more cowbell

Abrasive Sponge

unread,
Sep 23, 2004, 5:21:08 PM9/23/04
to
Bryce wrote:


Yeah, sorry, I am just wondering everybody thinks of that idear is all.

Jacob

unread,
Sep 24, 2004, 5:07:13 AM9/24/04
to
John Davison wrote:

> Do you mean this?
>
> public static setCount(int count) {
> Person.count = count;
> }
>
> public static int getCount() {
> return Person.count;
> }

I guess the question is wether this syntax should be
mandatory or not. I think it should. At least the
compiler should give a compilation warning when
the "Class." prefix on static elements is omitted.

Same of course when calling the methods:

Person.setCount(count);
Person.getCount();

Both from other classes and from within same class.

Chris Uppal

unread,
Sep 24, 2004, 5:13:01 AM9/24/04
to
Abrasive Sponge wrote:

> public static int getCount() {
> return static.count; //using a static keyword as such
> }
> }

Personally, I rather like this idea.

I've speculated for a long time that Java could use a "thisClass" notation of
some kind. It would eliminate a large chunk of completely pointless verbosity.

However I think that an explicit "thisClass" notation would probably work
better, in the sense that it could be used in more circumstances without
confusion.

For instance, consider a typical Singleton implementation:

class MyClass
{
private static final MyClass singleton = new MyClass();

public static MyClass
getSingleton()
{
return MyClass.singleton;
}
}

using "thisClass" in the above code would eliminate a great deal of redundancy:

class MyClass
{
private static final thisClass singleton = new thisClass();

public static thisClass
getSingleton()
{
return thisClass.singleton;
}
}

but I'm not sure that overloading "static" to the same extent is feasible:

class MyClass
{
private static final static singleton = new static();

public static static
getSingleton()
{
return static.singleton;
}
}

As I'm sure you'll agree ;-)

BTW, I'm not saying that "thisClass" wouldn't have problems too, e.g. what
would be the declared return type of a method overriding

public thisClass aMethod();

, would it be the parent class or the subclass ?

-- chris


Alex Hunsley

unread,
Sep 25, 2004, 5:36:57 AM9/25/04
to
Chris Uppal wrote:
> Abrasive Sponge wrote:
>
>
>>public static int getCount() {
>>return static.count; //using a static keyword as such
>>}
>>}
>
>
> Personally, I rather like this idea.
>
> I've speculated for a long time that Java could use a "thisClass" notation of
> some kind. It would eliminate a large chunk of completely pointless verbosity.
>
> However I think that an explicit "thisClass" notation would probably work
> better, in the sense that it could be used in more circumstances without
> confusion.
>
> For instance, consider a typical Singleton implementation:
>
> class MyClass
> {
> private static final MyClass singleton = new MyClass();
>
> public static MyClass
> getSingleton()
> {
> return MyClass.singleton;
> }
> }
>
> using "thisClass" in the above code would eliminate a great deal of redundancy:


What is wrong with writing:

getSingleton()
{
return singleton;
}

even less verbosity!
alex

Tor Iver Wilhelmsen

unread,
Sep 26, 2004, 3:02:35 AM9/26/04
to
Alex Hunsley <la...@tardis.ed.ac.molar.uk> writes:

> What is wrong with writing:
>
> getSingleton()
> {
> return singleton;
> }

If the goal is to remove Sun's error of allowing referencing static
members via instances, that's really "return this.singleton", and
hence bad since it uses an instance reference implicitly.

Chris Uppal

unread,
Sep 27, 2004, 5:14:50 AM9/27/04
to
Alex Hunsley wrote:

> > For instance, consider a typical Singleton implementation:

[...]


> > using "thisClass" in the above code would eliminate a great deal of
> > redundancy:
>
>
> What is wrong with writing:
>
> getSingleton()
> {
> return singleton;
> }
>
> even less verbosity!

A: It doesn't solve the general problem of excessively redundant reiteration of
the class name everywhere.

B: I'd prefer not to have to refer to a static directly like that. I admit I
do it, but that's /because/ of the lack of an explicit, but not redundant,
syntax.

Incidentally, the lack of explicitness is why I prefer to tag the names of
static and instance variable with 's_' and 'm_' respectively. I know that some
people don't like that, but I think they are confusing the idea with either the
similar but badly misguided practice of tagging them with 's' or 'm' (note the
lack of separating underscore), or with the, even worse, practice of tagging
the names of local variables. (Or even with Hungarian notation -- which is
quite simply an offence against humanity...)

-- chris


Bryce

unread,
Sep 27, 2004, 2:18:35 PM9/27/04
to
On Thu, 23 Sep 2004 15:21:08 -0600, Abrasive Sponge
<theabrasio...@yourhouse.com> wrote:

>Bryce wrote:
>
>> On Thu, 23 Sep 2004 13:48:59 -0600, Abrasive Sponge
>> <theabrasio...@yourhouse.com> wrote:

>>>I was wondering if the java language can use something
>>>like this....maybe there is something that I don't know, but having a
>>>static reference like this would kick butt.
>>
>>
>> uhhh... Would this do?
>>
>> public class Person {
>> private String firstName;
>> private static int count;
>>
>> public static void setCount(int count) {
>> Person.count = count;
>> }
>>
>> public static int getCount() {
>> return count;
>> }
>>
>> public static void main(String[] args) {
>> Person.setCount(100);
>>
>> System.out.println("getting count [" +
>> Person.getCount() + "]");
>> }
>> }
>>
>> or am I missing something in your request...
>
>

>Yeah, sorry, I am just wondering everybody thinks of that idear is all.

No problem with it, in fact, that's what static variables are for.

0 new messages