This question already has answers here:
Implementing toString on Java enums
(4 answers)
Closed 8 years ago.
I'm new to Java and I'm learning the language fundamentals.
Can someone explain to me how the toString method is called when there is no function call to it? I think it has something to do with the actual enumerator words on the second line such as:
KALAMATA("Kalamata"), LIGURIO("Ligurio") ...
The whole purpose for this enum class is so the ENUM values don't print to screen in all upper case characters.
Can someone please explain me how toString method is used in this class? Like when is it called? How is it called?
public enum OliveName {
KALAMATA("Kalamata"),LIGURIO("Ligurio"),PICHOLINE("Picholine"),GOLDEN("Golden");
private String nameAsString;
//for enum classes, the constructor must be private
private OliveName(String nameAsString) {
this.nameAsString = nameAsString;
}
#Override
public String toString() {
return this.nameAsString;
}
}
Pretty much like any object.
OliveName oliveName = OliveName.KALAMATA;
System.out.println(oliveName.toString());
or
System.out.println(oliveName);
Related
This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Closed 8 years ago.
I was reading a book and came across the term Shadow Variables in Java but there was no description for it. Eventually what are these variables used for and how are they implemented?
Instead of providing my own description i may ask you to read about it for example here: http://en.wikipedia.org/wiki/Variable_shadowing. Once you understood the shadowing of variables i recommend you proceed reading about overlaying/ shadowed methods and visibility in general to get a full understanding of such terms.
Actually since the question was asked in Terms of Java here is a mini-example:
public class Shadow {
private int myIntVar = 0;
public void shadowTheVar(){
// since it has the same name as above object instance field, it shadows above
// field inside this method
int myIntVar = 5;
// If we simply refer to 'myIntVar' the one of this method is found
// (shadowing a seond one with the same name)
System.out.println(myIntVar);
// If we want to refer to the shadowed myIntVar from this class we need to
// refer to it like this:
System.out.println(this.myIntVar);
}
public static void main(String[] args){
new Shadow().shadowTheVar();
}
}
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 4 years ago.
Suppose I have three constructors in a class:
public class MyClass {
Parent thingA;
Child thingB;
boolean someBoolean;
double someDouble;
double anotherDouble;
public MyClass(Parent thingA, boolean someBoolean) {
this(thingA, someBoolean, 0.25);
}
public MyClass(Child thingB, boolean someBoolean, double anotherDouble) {
// Casting to prevent recursive constructor call
this((Parent) thingB, someBoolean, 0.5);
this.thingB = thingB;
this.anotherDouble = anotherDouble;
}
public MyClass(Parent thingA, boolean someBoolean, double someDouble) {
this.thingA = thingA;
this.someBoolean = someBoolean;
this.someDouble = someDouble;
}
}
Where Child extends Parent. My question is, when I call another constructor from a constructor in Java via this(...), am I passing by reference or passing by value? How does casting affect it? If I were to use the second constructor and modify a property of thingB, would that reflect in thingA?
Edit: I have both thingA and thingB fields because sometimes my class will be used without an instance of Child, but when Child is used, I need to take advantage of specific behaviors of Child.
In Java, all objects (non-primitive types) are passed by reference. Casting has no effect on this behavior. If this isn't what you want, you can create a copy of your object (you'll need to implement this behavior yourself, usually either as a method or constructor) and then pass that copy instead.
This question already has answers here:
How to use the toString method in Java?
(13 answers)
Closed 8 years ago.
I've been given the following array
tests[] b = new tests[50];
So, there are some null elements in this array and I don't want to print those in my array.
for(int i = 0; i < b.length; i++){
if(b[i] != null){
System.out.println(b[i]);
}
}
So, this prints out '#251970e2' but I need to be able to print out each valid elements contents which should be like 'batman', 'joker', 'batgirl'
Sorry if this has been answered previously, I had a look but haven't had much luck :(
You need to override toString method in your class because it is going to give you clear information about the object in readable format that you can understand.
The merit about overriding toString:
Help the programmer for logging and debugging of Java program
Since toString is defined in java.lang.Object and does not give valuable information, so it is
good practice to override it for subclasses.
#override
public String toString(){
// I assume name is the only field in class test
return name ;
}
Override toString method in your Tests class ..
class Tests{
..... your code
#Override
public String toString(){
... return the value
}
}
You need to override the toString() method in your tests class.
For further discussion, see How to use the toString method in Java?
This question already has answers here:
Does Java have "properties" that work the same way properties work in C#?
(5 answers)
Closed 9 years ago.
Is there an equivalent of the C# property call in java?
protected int foo2{ get; set; }
instead of doing this all the time:
private int foo2;
public void setfoo2 (int value) {foo2 = value;}
public int getfoo2 () {return foo2;}
nope.
they are different languages. different ways of doing things. BTW c# too does the same thing in the background. what c# gives is what we call syntactic sugar. it gives a shorthand writing.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is Java pass by reference?
I need to modify the content of a variable that is passed to a function within that function.
The type is String. I want to inject a preceeding char when using the insertString function of an extendeded class of PlainDocument.
Use a wrapper class:
public class Wrapper
{
public String text;
}
// ...
public static void changeString(Wrapper w, String newText)
{
w.text = newText;
}
You can use it like:
Wrapper w = new Wrapper();
w.text = "old text";
changeString(w, "new text");
System.out.print(w.text);
OUTPUT:
new text
Also see this answer: https://stackoverflow.com/a/9404727/597657
The short answer is no, but you can always "simulate" the pointer by using an intermediate object.