I've been trying to learn Java and I'm working on a project.
I have an abstract class called Card that I'm using as a template to make sub-classes.
It has a method to set the name field and color field of the card and a method to set the cost.
I have a subclass of Card called Workshop.
I'm trying to use the methods of Card in Workshop to set the name and color fields of Workshop.
To do so, I'm calling the methods super.setCardNameColor("Workshop", "Green")
and super.setCardCost(0,0,0,0,0,0,0,0);
However, in order to do this Eclipse in making me use (what appears to be) double curly braces in the class declaration.
I suspect this has something to do with Anonymous Inner Classes, perhaps from calling 'super' or something like that, but none of my Google searches are giving me the information I need. Can anyone shed some light on the subject here? I'm using sysout and getters to make sure that the values are being set correctly on an instance of Workshop, but I'm baffled by the double braces. Thank you ahead of time for your help!
Edit: here is the code
public abstract class Card
{
private String cardName = "";
private String cardColor = "";
private int coinCost = 0;
private int woodCost = 0;
private int brickCost = 0;
private int stoneCost = 0;
private int oreCost = 0;
private int glassCost = 0;
private int clothCost = 0;
private int paperCost = 0;
private int coinValue = 0;
private int pointValue = 0;
private int millitaryValue = 0;
private int woodValue = 0;
private int brickValue = 0;
private int stoneValue = 0;
private int oreValue = 0;
private int glassValue = 0;
private int clothValue = 0;
private int paperValue = 0;
private Card discountForCard;
private Card discountFromCard;
public void setCardNameColor(String cardName, String cardColor) {
this.cardName = cardName;
this.cardColor = cardColor;
}
public void setCardCost(int coinCost, int woodCost, int brickCost,
int stoneCost, int orecost, int glassCost, int clothCost,
int paperCost) {
this.coinCost = coinCost;
this.woodCost = woodCost;
this.brickCost = brickCost;
this.stoneCost = stoneCost;
this.oreCost = orecost;
this.glassCost = glassCost;
this.clothCost = clothCost;
this.paperCost = paperCost;
}
public void setCardValue(int coinValue, int millitaryValue, int pointValue,
int woodValue, int brickValue, int stoneValue, int oreValue,
int glassValue, int clothValue, int paperValue) {
this.coinValue = coinValue;
this.millitaryValue = millitaryValue;
this.pointValue = pointValue;
this.woodValue = woodValue;
this.brickValue = brickValue;
this.stoneValue = stoneValue;
this.oreValue = oreValue;
this.glassValue = glassValue;
this.clothValue = clothValue;
this.paperValue = paperValue;
}
public void getCardInfo() {
System.out.println(cardName + cardColor + coinCost + woodCost+brickCost+stoneCost+oreCost+glassCost+clothCost+paperCost);
}
public void setGivesDiscountTo(Card card) {
card = discountForCard;
}
public void setReceivesDiscountFrom(Card card) {
card = discountFromCard;
}
public String getCardName() {
return cardName;
}
}
public class Workshop extends Card
{
{
super.setCardNameColor("Workshop", "Green");
super.setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
}
}
Double brace initialization is a combination of two separate process in java. There are two { braces involved in it. If you see two consecutive curly braces { in java code, it is an usage of double brace initialization. Java Double Brace Initialization. First brace is creation of an anonymous inner class.
The simplest and global way: Go to Window -> Preferences. Then in search put: "brace". Select -> Java -> Code Style-> Formatter After opening new window go to brace tab and change it as you prefer.
In Java when you open a curly brace it means that you open a new scope (usually it's a nested scope).
Always include curly brackets. Always include curly brackets, even for one-line if statements or for loops. Java has a "feature" which allows you to omit curly brackets when writing if statements, for loops, or while loops containing only a single statement.
The inner braces in the "double curly braces" are initializer blocks. (Think of them as a constructor.)
<class name> { // class declaration
{ // initializer block
...
}
}
See for instance What is an initialization block?.
Since you can't put statements ("code") directly in a class declaration, like this:
new YourClass() { System.out.println("hello"); }
you experience that "Eclipse makes you use double curly braces" because the following
new YourClass() {{ System.out.println("hello"); }}
makes the statement go in the initializer block with is valid code.
Regarding your edit: Your problem is here:
public class Workshop extends Card {{
super.setCardNameColor("Workshop", "Green");
super.setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
}}
This isn't very common style of programming. Perhaps you're after:
public class Workshop extends Card {
public Workshop() {
setCardNameColor("Workshop", "Green");
setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With