Re: Convert String to Object - Newbie Question
- From: Hendrik Maryns <hendrik_maryns@xxxxxxxxxxxxx>
- Date: Fri, 28 Sep 2007 16:55:28 +0200
Marion schreef:
Hello all,
I am trying to convert a myString declared as a String[] and convert
it into an Object so that I can use the non static method.
Currently, my program does not compile. The error I'm receiving is my
old friend error msg: "non-static method setData....cannot be
referenced from a static content.
Please post the whole error message, especially the line numbers, and
provide us with compilable code which exhibits the problem. Google for
SSCCE. Maybe your problem will be solved simply by constructing one.
Essentially, I want to read in an array of Strings into objects for
data manipulation (a sort to be exact.).
The program looks something like this:
...
public void init (String[] array, int size){
.// assume array[0] is the first string read in from the text file
String dataString = array[0];
Node.setData(dataString);
...
}
Class Node {
(constructor stuff declared here)
}
...
public void setData(Object data){
this.data = data;
}
This does not compile and does not mention the type of ‘data’, so we
cannot help you.
I read from the Java docs that String was an object (or was extended
by it) so doesn't that mean that String is an object? I'm beginning
to dislike objects! lol,
A String is an Object indeed. You won’t get far if you only want to use
primitive types.
I know how to turn integers into objects with:
Integer one = new Integer(1);
Not a good idea! Use Integer.valueOf(int)
why doesn't "String myString = new String (array[0]);:
work?
Did you try it? It does, provided array[0] is of the right type (again,
you didn’t give a clue about that). However, in 99% of the cases it is
not a good idea to use the String constructor explicitly. Google for
‘java string internalisation’ if you want the details.
I tried to read documentation for java.io.Serializable (not sure if
that's what it's called but something like that) and on reflections.
being new to java, those documents I read scared the begeezus off of
me. (didn't understand em, nosiree)
Those are rather advanced concepts for a beginner. Why don’t you start
with the easy stuff?
I tried to use "stringToObject(string,class)" statement and sadly, I
am having trouble understanding how to use it in my program. There
are no examples anywhere.
And where is that method supposed to live?
All I want is to convert a string to object and it has gotten suddenly
complicated. :(
There is no conversion needed. A String *is* an Object. So:
String s = "my string";
Object o = s;
works.
Ok, well it's 4am here, I'll try this again in the morning. :)
Anyone with hints or suggestions, please, I'm open to them.
I hope your next message will be better. Consider reading the link in
my signature.
H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
.
- References:
- Convert String to Object - Newbie Question
- From: Marion
- Convert String to Object - Newbie Question
- Prev by Date: Convert String to Object - Newbie Question
- Next by Date: Re: Convert String to Object - Newbie Question
- Previous by thread: Convert String to Object - Newbie Question
- Next by thread: Re: Convert String to Object - Newbie Question
- Index(es):
Relevant Pages
|
|