newbie Integer class question

G

gary

From what I can tell, it seems that the Integer class wraps the
primitive integer datatype and provides methods like parseInt to
change strings into Integers.

Why is this method available when I haven't instantiated an object?
 
D

David Lamb

From what I can tell, it seems that the Integer class wraps the
primitive integer datatype and provides methods like parseInt to
change strings into Integers.

Why is this method available when I haven't instantiated an object?

parseInt is "static", meaning it's part of the class itself rather than
part of any instance. It's essentially a standard procedure in a
procedural (non-object-oriented) language like C.
 
M

Mayeul

From what I can tell, it seems that the Integer class wraps the
primitive integer datatype and provides methods like parseInt to
change strings into Integers.

Actually into ints. Auto-Inboxing enabling to transparently box said int
into an Integer.

valueOf() will provide Integers directly.
Why is this method available when I haven't instantiated an object?

How is that possible:
Because this method is static.

Why is it made so:
Because there is no reason to require an Integer instance when you need
to parse a String into an int.
 
B

BGB

From what I can tell, it seems that the Integer class wraps the
primitive integer datatype and provides methods like parseInt to
change strings into Integers.

Why is this method available when I haven't instantiated an object?

because it is 'static'...

anything static is available at the class level and does not need an
instance, and this applies to both methods and fields.

however, if one needs to access instance variables, then static is
generally not used (using a static method would require passing the
object explicitly, whereas a non-static method receives the object as an
implicit 'this' variable).

or such...
 
G

gary

because it is 'static'...

anything static is available at the class level and does not need an
instance, and this applies to both methods and fields.

however, if one needs to access instance variables, then static is
generally not used (using a static method would require passing the
object explicitly, whereas a non-static method receives the object as an
implicit 'this' variable).

or such...

Cool... thanks everyone for the responses.
 
J

Jukka Lahtinen

Mayeul said:
On 18/11/2010 18:25, gary wrote:
How is that possible:
Because this method is static.
Why is it made so:
Because there is no reason to require an Integer instance when you need to
parse a String into an int.

And one more reason for especially Integer.valueOf(String) being static:
An Integer object is immutable. That means you can't change its value
after initialization.
So, there would be no point in first having to instantiate one Integer
object with some irrelevant value just to extract a value from a String
as yet another Integer object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top