Hello there..
I.m trying to learn Java from books, the net and other assorted
materials..
Beginning to work through Thinking in Java 3d ed.
I don`t understand the following.
The exercise is to write a program using this method:
int storage(String s) {
return s.length() * 2;
}
so i came up with:
1 class StringLen{
2 int storage(String s) {
3 return s.length() * 2;
4 }
5
6 public static void main( String[] args) {
7 String a = "hello world!";
8 System.out.println (storage (a));
9 }
10 }
Now this doesn`t work..
It wil if I insert static before declaring the return type of
storage.(line 2)static int storage(etc
I also tried to make the StringLen class hold a string i
and using new Storage a the type for String a.
But then the compiler says that i can`t use storage on StringLen.
Could someone explain why i can`t use storage on a string without
static and what does it take to get a string to storage without it
beeing static?
Thanks Catsquotl
Christophe Vanfleteren - 09 Apr 2004 11:24 GMT
> Hello there..
>
[quoted text clipped - 37 lines]
>
> Thanks Catsquotl
You'll need to understand the difference between static and normal
variables/methods.
There's a part in the tutorial about it here:
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
Short explanation: you need an instance of a class to invoke a non-static
(normal) method on it.
A static method on the other hand, can be executed without using an instance
of the class:
Consider the following:
new SomeObject().someNonStaticMethod();
vs.
SomeObject.someStaticMethod();
You'll never be able to use SomeObject.someNoneStaticMethod since it
requires an actual instance of the SomeObject class.
Now as your case, you just need to make sure you create an instance of your
StringLen class, before you call the method:
public static void main( String[] args) {
String a = "hello world!";
StringLen sl = new StringLen();
System.out.println (sl.storage(a));
}

Signature
Kind regards,
Christophe Vanfleteren
Mark Haase - 09 Apr 2004 14:55 GMT
> so i came up with:
>
[quoted text clipped - 8 lines]
> 9 }
> 10 }
"Static" means the method is run in the context of the class itself, not
in the context of any particular instance of the class. Notice that the
main method is static (it has to be)...this means there's no object
associated with it.
What you should do is create an object and then call the storage method
on that.
public static void main( String[] args) {
String a = "hello world!";
StringLen sl = new StringLen();
System.out.println (sl.storage(a));
}
Of course, since storage() is a utility function (i mean, it only
operates on the parameter passed to it, independent of any object), it
makes sense for it to be static, as you wrote in your first post.
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Bryce (Work) - 09 Apr 2004 16:34 GMT
>Hello there..
>
[quoted text clipped - 24 lines]
>
>Now this doesn`t work..
That's because storage is a method, and you need an instance of the
class to perform that method. You might also want to make it public...
>It wil if I insert static before declaring the return type of
>storage.(line 2)static int storage(etc
That's because static make it a class method, not needing an instance
of the class to use the method. Check out the book on static methods
and variables.
>I also tried to make the StringLen class hold a string i
>and using new Storage a the type for String a.
[quoted text clipped - 6 lines]
>
>Thanks Catsquotl
--
now with more cowbell
Roedy Green - 09 Apr 2004 19:34 GMT
>Now this doesn`t work..
>It wil if I insert static before declaring the return type of
>storage.(line 2)static int storage(etc
That is what a sane person would do. It is naturally a static method.
However, the exercise seem to want you to put the method in some dummy
object, and then invoke it. you need to learn about NEW.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.