Good idea or full of it?

A

Abrasive Sponge

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.
 
J

John Davison

Abrasive said:
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.

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
 
A

Abrasive Sponge

John said:
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. :)
 
B

Bryce

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.

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...
 
A

Abrasive Sponge

Bryce said:
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.


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.
 
J

Jacob

John said:
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.
 
C

Chris Uppal

Abrasive said:
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
 
A

Alex Hunsley

Chris said:
Abrasive Sponge wrote:




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
 
T

Tor Iver Wilhelmsen

Alex Hunsley said:
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.
 
C

Chris Uppal

Alex said:
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
 
B

Bryce

Bryce said:
On Thu, 23 Sep 2004 13:48:59 -0600, Abrasive Sponge
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.
 

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

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top