I'm trying to get a string from the content of an object with a custom method in my Android application. The object is an instance of the class Game and is contained in an arraylist:
public class Game {
private static List<Game> games = new ArrayList<Game>();
private String teamName1;
private String teamName2;
private int goalsP1;
private int goalsP2;
private String result;
public Game(String teamName1, String teamName2, int goalsP1, int goalsP2)
{
this.teamName1 = teamName1;
this.teamName2 = teamName2;
this.goalsP1 = goalsP1;
this.goalsP2 = goalsP2;
}
public static List getGames() {
return games;
}
public static void addGameToArray(Game randGame) { games.add(randGame);}
public static String getStringFromGame(Game randGame) {
randGame.result = randGame.result + randGame.teamName1 + randGame.goalsP1 + randGame.goalsP2 + randGame.teamName2;
return randGame.result;
}
In my MainActivity I'm trying to call the getStringFromGame:
Game game1 = new Game("Man City ","Real Madrid", 5 ,0);
Game.addGameToArray(game1);
String result = Game.getStringFromGame(Game.getGames().get(0));
I get an error:
getStringFromGame cannot be applied to java.lang.Object
What is going wrong?
It looks like you're returning a generic list with getGames().
So define it as
public static List<Game> getGames() { return games; }
and everything should work fine.
Java is statically typed. This error message translates to "You have passed an Object to getStringFromGame, which we know takes a Game.
You don't show your getGames definition, but the error message indicates you're returning an Object value from it. Because Game extends Object (and not the other way around), you have a problem. A function that is expecting a Game will reject being passed an Object.
Given the get(0) here:
String result = Game.getStringFromGame(Game.getGames().get(0));
The return type of getGames() is probably a Collection of some sort. If the type of a collection is not specified, Java assumes everything in that collection is of type Object. Java Generics can be used to define what it is a collection of. So rather than a signature like this:
public List getGames() { ... }
You want something like this:
public List<Game> getGames() { ... }
(Note that List is a subtype of Collection. If you care about order, then use List, otherwise you should replace it with Collection).
Further, it seems to indicate that you are randomizing the order of that list. In which case an even better solution would be:
public Game getRandomGame() { ... }
Which avoids the issue of generics altogether and keeps the randomization encapsulated.
Post-Update Addendum
As a minor aside, this might get you into trouble:
public class Game {
private static List<Game> games = new ArrayList<Game>();
...
public static List getGames() {
return games;
}
...
}
Because you're declaring games as a static member field, any instantiated game may update that. This can get confusing and hard to predict very fast. It might be better to put these in a separate class (GameLibrary?) like so:
public class GameLibrary {
private List<Game> games = new ArrayList<Game>();
public List getGames() {
return games;
}
}
This way, code using one instance of GameLibrary has no access to every other instance of GameLibrary. As it stands, any instantiation of Game can modify your the private static List<Game> games value... which breaks encapsulation.
Your getGames() method obviously returns a List<Something?!> instead of a List<Game>. You can't pass an argument that is not a Game or it's subclass to the getGames() method since it expects a Game to be passed.
Just make the method return a List<Game> and it'll work.
I fell over a class which looks like this:
public final class DatabaseType{
public static final DatabaseType TYPE_LIMITED_TEXT = new DatabaseType();
public static final DatabaseType TYPE_UNLIMITED_TEXT = new DatabaseType();
public static final DatabaseType TYPE_DATE = new DatabaseType();
public static final DatabaseType TYPE_DECIMAL = new DatabaseType();
private DatabaseType(){
}
public String toString(){
return "DatabaseType";
}
}
I need to set the type but I want to understand what's happening here and I have no clue how this class works.
Whatever variable I use it will always return an empty DatabaseType, with no information. So I wonder how you can get use of such a class. Maybe there is a name for this type of class?
Basically, the class lists four enumerable constants, which you can use like this in method signatures
public DatabaseType getTypeOfDB();
In client code, you'll have a type-safe way to compare the constants:
if (getTypeOfDB() == DatabaseType.TYPE_LIMITED_TEXT) {
doSomething();
}
Even though the implementation seems a bit clumsy, it quite closely emulates a Java 5 enum, as Gimby pointed out in the comments. The good ideas in the design are the following:
The constructor is private, meaning only the public static final DatabaseType instances declared within the class can exist
The class is final so you cannot work around the above restriction by adding more constants in a subclass
The constant fields in the class have strong typing, i.e. they are not ints, but instead DatabaseTypes, which helps to eliminate bugs caused by typos or "magic numbers" in client code
The modern way to do the same would be using an enum instead:
public enum DatabaseType {
TYPE_LIMITED_TEXT, TYPE_UNLIMITED_TEXT, TYPE_DATE, TYPE_DECIMAL;
}
If you use call the function toString() you will always get the String : DatabaseType.
As i can understand you want to return the name of the variable you created that are DatabaseType.
Create a variable private String name; and modify the constructor like this:
private DatabaseType(String name){
this.name = name;
}
Also create a function
public String getName(){
return this.name;
}
Finally, when you create a databaseType object create it like this:
public static final DatabaseType TYPE_LIMITED_TEXT = new DatabaseType("TYPE_LIMITED_TEXT");
When I create:
player huhu = new player();
I want to get "huhu" to String name inside player;
public class player{
String name = ??? How to get "huhu" here?
....
}
Sorry for my poor english!
class Player {
private String name;
public Player(String s) {
name = s;
}
}
Player huhu = new Player("huhu");
Notice that I'm capitalizing the name of the class.
You should always capitalize your class names.
No, you can't access the local variables' names using standard Java.
If you really need access to the name, you'll have to pass it as a constructor parameter, as specified in some of the comments. That would require you, however, to change your code to support this for each and every variable you declare.
I'm seriously scared to ask why you need this functionality.
Hello I would like to create a Class which contains an array with the same class.
I tried with the code specified below, but the array i created has been infinite.
public class State {
private String Valor1;
private String Valor2;
private ArrayList arrayStatesAnteriores;
}
And i did the set and get with the refactor of netbeans
state.setArrayStatesAnteriores(arrayStateAnteriores);
But i have the problem of array being infinite. Any idea?
Here's the way I understand it: You have a class that represents a program's state in a given moment, and you want to keep a list of the state that the program has had, that's why you say it's an infinite list.
First the State class, which has two fields value1 and value2 one constructor which sets the two fields to the passed values:
public class State {
/**
* This is the constructor
*/
public State(String value1, String value2){
this.value1 = value1;
this.value2 = value2;
}
// Omiting getters/setters for brevety.
// This will be set by the constructor using the values that it
// receives as arguments
// e.g
// new State("My Val1","My Val2");
private String value1;
private String value2;
}
Then this class would be contained in an array of States
List<State> states = new ArrayList<States>();
Then you would use this classes in your main or other classes:
import State;
public class Program {
// This array holds the states that the program has had.
private static List<State> states = new ArrayList<State>();
public static void main(String[] args){
// ...
// Do something
// ...
// Save the states
states.add(new State("State 1","Value 1"))
// Save another state
states.add(new State("State 2","Value 2"))
// The arraylist now contains two states of the program.
}
}
Hope it helps feel free to ask more questions.
Use a List, and initialize it:
public class State {
private List<State> statesAnteriores = new ArrayList<State>();
}
First of all, just a few tweeks on your code:
ArrayList is just in implementation of the List interface. You don't have to use it when defining a member field.
you should define the type of the list elements, see generics in Java: http://docs.oracle.com/javase/tutorial/java/generics/index.html
so that would be
public class State {
private String Valor1;
private String Valor2;
private List<State> arrayStatesAnteriores;
}
And regarding your question, what do you mean by " the array is infinite"?
You mean it is null after you have set it?
state.setArrayStatesAnteriores(arrayStateAnteriores);
the reason for this may be you setting the value of the list with itself, which is null.
try something like this:
state.setArrayStatesAnteriores(new ArrayList<State>());
First of all I should probably say that the term 'constant object' is probably not quite right and might already mean something completely different from what I am thinking of, but it is the best term I can think of to describe what I am talking about.
So basically I am designing an application and I have come across something that seems like there is probably an existing design pattern for but I don't know what it is or what to search for, so I am going to describe what it is I am trying to do and I am looking for suggestions as to the best way to implement it.
Lets say you have a class:
public class MyClass {
private String name;
private String description;
private int value;
public MyClass(String name, String description, int value) {
this.name = name;
this.description = description;
this.value = value;
}
// And I guess some getters and setters here.
}
Now lets say that you know in advance that there will only ever be say 3 instances of this class, and the data is also known in advance (or at least will be read from a file at runtime, and the exact filename is known in advance). Basically what I am getting at is that the data is not going to be changed during runtime (once it has been set).
At first I thought that I should declare some static constants somewhere, e.g.
public static final String INSTANCE_1_DATA_FILE = "path/to/instance1/file";
public static final String INSTANCE_2_DATA_FILE = "path/to/instance2/file";
public static final String INSTANCE_3_DATA_FILE = "path/to/instance3/file";
public static final MyClass INSTANCE_1 = new MyClass(getNameFromFile(INSTANCE_1_DATA_FILE), getDescriptionFromFile(INSTANCE_1_DATA_FILE), getValueFromFile(INSTANCE_1_DATA_FILE));
public static final MyClass INSTANCE_2 = new MyClass(getNameFromFile(INSTANCE_2_DATA_FILE), getDescriptionFromFile(INSTANCE_2_DATA_FILE), getValueFromFile(INSTANCE_2_DATA_FILE));
public static final MyClass INSTANCE_3 = new MyClass(getNameFromFile(INSTANCE_3_DATA_FILE), getDescriptionFromFile(INSTANCE_3_DATA_FILE), getValueFromFile(INSTANCE_3_DATA_FILE));
Obvisouly now, whenever I want to use one of the 3 instances I can just refer directly to the constants.
But I started thinking that there might be a cleaner way to handle this and the next thing I thought about was doing something like:
public MyClassInstance1 extends MyClass {
private static final String FILE_NAME = "path/to/instance1/file";
public String getName() {
if (name == null) {
name = getNameFromFile(FILE_NAME);
}
return name;
}
// etc.
}
Now whenever I want to use the instances of MyClass I can just use the one I want e.g.
private MyClass myInstance = new MyClassInstance2();
Or probably even better would be to make them singletons and just do:
private MyClass myInstance = MyClassInstance3.getInstance();
But I can't help but think that this is also not the right way to handle this situation. Am I overthinking the problem? Should I just have a switch statement somewhere e.g.
public class MyClass {
public enum Instance { ONE, TWO, THREE }
public static String getName(Instance instance) {
switch(instance) {
case ONE:
return getNameFromFile(INSTANCE_1_DATA_FILE);
break;
case TWO:
etc.
}
}
}
Can anyone tell me the best way to implement this? Note that I have written the sample code in Java because that is my strongest language, but I will probably be implementing the application in C++, so at the moment I am more looking for language independent design patterns (or just for someone to tell me to go with one of the simple solutions I have already mentioned).
If you want the values to be constant, then you will not need setters, otherwise code can simply change the values in your constants, making them not very constant. In C++, you can just declare the instances const, although I'd still get rid of the setters, since someone could always cast away the const.
The pattern looks ok, although the fact that you are creating a new instance each time one is requested, is not usual for constants.
In java, you can create enums that are "smart" e.g.
public enum MyClass {
ONE(INSTANCE_1_DATA_FILE),
TWO(INSTANCE_2_DATA_FILE),
//etc...
private MyClass(String dataFile)
{
this(getNameFromDataFile(dataFile), other values...)
}
private MyClass(String name, String data, etc...)
{
this.name = name;
// etc..
}
public String getName()
{
return name;
}
}
In C++, you would create your MyClass, with a private constructor that takes the filename and whatever else it needs to initialize, and create static const members in MyClass for each instance, with the values assigned a new instance of MyClass created using the private constructor.
EDIT: But now I see the scenario I don't think this is a good idea having static values. If the types of ActivityLevel are fundamental to your application, then you can enumerate the different type of activity level as constants, e.g. a java or string enum, but they are just placeholders. The actual ActivityDescription instances should come from a data access layer or provider of some kind.
e.g.
enum ActivityLevel { LOW, MED, HIGH }
class ActivityDescription
{
String name;
String otherDetails;
String description; // etc..
// perhaps also
// ActivityLevel activityLevel;
// constructor and getters
// this is an immutable value object
}
interface ActivityDescriptionProvider
{
ActivityDescription getDescription(ActivityLevel activityLevel);
}
You can implement the provider using statics if you want, or an enum of ActivityDescription instnaces, or better still a Map of ActivityLevel to ActivityDescription that you load from a file, fetch from spring config etc. The main point is that using an interface to fetch the actual description for a given ActivityLevel decouples your application code from the mechanics of how those descriptions are produced in the system. It also makes it possible to mock the implementation of the interface when testing the UI. You can stress the UI with a mock implementation in ways that is not possible with a fixed static data set.
Now lets say that you know in advance that there will only ever be say 3 instances of this class, and the data is also known in advance (or at least will be read from a file at runtime, and the exact filename is known in advance). Basically what I am getting at is that the data is not going to be changed during runtime (once it has been set).
I'd use an enum. And then rather in this flavor:
public enum MyEnum {
ONE("path/to/instance1/file"),
TWO("path/to/instance2/file"),
THREE("path/to/instance3/file");
private String name;
private MyEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Which can be used as follows:
MyEnum one = MyEnum.ONE;
String name = one.getName();
(I'm too slow once again, you already accepted an answer, but here it is anyway...)
You want to (a) prevent changes to the data held in objects of MyClass, and (b) allow only a fixed set of MyClass objects to exist, implying that runtime code should not be able to create new instances of MyClass.
Your initial example has a public constructor, which violates (b)
I'd use a Factory approach so the Factory is the only thing that can create instances, and the class doesn't provide any setters so it's immutable.
Depending on how much flexibility you want for the future, you could put the factory and the class in the same package and limit scope that way, or you could make MyClass an inner class within the factory. You may also consider making MyClass an interface separate from its implementation.
A properties file could be used to configure the factory itself.
The properties file (e.g. "foo.properties") could look something like
one=/path/to/datafile1
two=/another/path/to/datafile2
three=/path/to/datafile3
I use "Foo" instead of "MyClass" in the (Java) examples below.
public class FooFactory
{
/** A place to hold the only existing instances of the class */
private final Map<String, Foo> instances = new HashMap<String, Foo>();
/** Creates a factory to manufacture Foo objects */
// I'm using 'configFile' as the name of a properties file,
// but this could use a Properties object, or a File object.
public FooFactory(String configfile)
{
Properties p = new Properties();
InputStream in = this.getClass().getResourceAsStream();
p.load(in); // ignoring the fact that IOExceptions can be thrown
// Create all the objects as specified in the factory properties
for (String key : p.keys())
{
String datafile = p.getProperty(key);
Foo obj = new Foo(datafile);
instances.put(key, obj);
}
}
public Foo getFoo(String which)
{
return instances.get(which);
}
/** The objects handed out by the factory - your "MyClass" */
public class Foo
{
private String name;
private String description;
private int value;
private Foo(String datafile)
{
// read the datafile to set name, description, and value
}
}
}
You're set to allow only your predefined instances, which can't be changed at runtime, but you can set it all up differently for another run at a later time.
Your first method seems to me like the best and the least prone to code rot. I'm not impressed by the idea of subclassing an object just to change the file name that contains the data that will be used to build it.
Of course, you could maybe improve on your original idea by wrapping these all in an outer class that provides some sort of enumeration access. A collection of MyClass's in other words. But I think you should discard this subclassing idea.
First, you really should be limiting where you use these instances in the code. Use them in as few places as possible. Given these are file names, I expect you want three class instances which accesses the files. How many classes are required depends on what your want to do with them? Look at the Singleton pattern for these classes.
Now you don't need the constants, but could have a helper class which will read the file containing the file names and supply them to the reader class. The code to find then name could also be a method called by the static initializer of the Singleton.
The common approach is to use a map:
private static final Map<String, YouClass> mapIt =
new HashMap<String, YouClass>(){{
put("one", new YourClass("/name", "desc", 1 )),
put("two", new YourClass("/name/two", "desc2", 2 )),
put("three", new YourClass("/name/three", "desc", 3 ))
}}
public static YourClass getInstance( String named ) {
return mapIt.get( named );
}
Next time you need it:
YouClass toUse = YourClass.getInstance("one");
Probably using strings as keys is not the best option but you get the idea.