Is this correct?

Z

ZOCOR

Hi

public class Apple
{
String colour;
String weight;

public void method1()
{}

public String toString()
{
String result = null;
return result = "something";
}
} //end

Question:

Should the declaration of result be at the instance level or local?


ZOCOR
 
M

Michael Borgwardt

ZOCOR said:
public class Apple
{
String colour;
String weight;

public void method1()
{}

public String toString()
{
String result = null;
return result = "something";
}
} //end

Question:

Should the declaration of result be at the instance level or local?

It shouldn't be declared at all:

public String toString()
{
return "something";
}
 
A

Andrew Thompson

public class Apple
{
// you are using the spelling of colour common
// to Australia, Britain and pther obscure places..
// Java spells it 'Color', which by the way,
// is (probably) the perfect type for this attribute
Color colour;
String weight;

public void method1()
{}

public String toString()
{
return "Apple - Color: " + colour
+ " \t Weight: " + weight;
}
} //end

Question:

Should the declaration of result be at the instance level or local?

What the heck did 'result' ever have to
do with the 'toString' method.. As you
can see, I replaced it completely.

Is this a theoretical question? Your
meaning is not clear to me.
 
S

sks

Andrew Thompson said:
// you are using the spelling of colour common
// to Australia, Britain and pther obscure places..

It's not our fault you can't spell correctly.
 
S

Stefan Siegl

ZOCOR said:
Hi

public class Apple
{
String colour;
String weight;

public void method1()
{}

public String toString()
{
String result = null;
return result = "something";
}
} //end

An additional hint: Strings are immutable, meaning that if you change a
String, internally a new String is created! Keep that in mind. If you do
a lot of operations on a String, use the StringBuffer class to represent
the String

Stefan
 
B

Bryce

Hi

public class Apple
{
String colour;
String weight;

public void method1()
{}

public String toString()
{
String result = null;
return result = "something";
}
} //end

Question:

Should the declaration of result be at the instance level or local?

Your preference. But its generally good practice to declare your
variables only when you need them. for instance, in your toString
method, you would want to declare it in the method. Now, given that
its a toString() method, it will probably be printing out variables
that are class level, such as the colour and weight variables you
have.
 
S

sks

Thomas Schodt said:
But he can

Color colour;

the spelling of the Java class is not his doing...

I was replying to Thompson dismissing the country that is the origin of the
language :)
 
T

Tony Morris

package spelling.correct;

class Colour extends Color
{
// All those constructors
}

// :)
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top