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.
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
Hahaha, I do that already :)
It's just a feature unused that makes sense, so I thought why not. :)
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
Yeah, sorry, I am just wondering everybody thinks of that idear is all.
> 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.
> 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
What is wrong with writing:
getSingleton()
{
return singleton;
}
even less verbosity!
alex
> 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.
> > 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 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.