newbie Integer class question

Discussion in 'Java' started by gary, Nov 18, 2010.

  1. gary

    gary Guest

    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?
     
    gary, Nov 18, 2010
    #1
    1. Advertising

  2. gary

    David Lamb Guest

    On 18/11/2010 12:25 PM, gary wrote:
    > 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.
     
    David Lamb, Nov 18, 2010
    #2
    1. Advertising

  3. gary

    Mayeul Guest

    On 18/11/2010 18:25, gary wrote:
    > 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.

    --
    Mayeul
     
    Mayeul, Nov 18, 2010
    #3
  4. gary

    BGB Guest

    On 11/18/2010 10:25 AM, gary wrote:
    > 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...
     
    BGB, Nov 18, 2010
    #4
  5. gary

    gary Guest

    On Nov 18, 10:32 am, BGB <> wrote:
    > On 11/18/2010 10:25 AM, gary wrote:
    >
    > >  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...


    Cool... thanks everyone for the responses.
     
    gary, Nov 19, 2010
    #5
  6. gary

    Roedy Green Guest

    On Thu, 18 Nov 2010 09:25:14 -0800 (PST), gary <>
    wrote, quoted or indirectly quoted someone who said :

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


    All static methods are available without an object. That is the whole
    point of static methods.


    see http://mindprod.com/jgloss/static.html

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Finding a bug is a sign you were asleep a the switch when coding. Stop debugging, and go back over your code line by line.
     
    Roedy Green, Nov 19, 2010
    #6
  7. gary

    Lew Guest

    gary wrote, quoted or indirectly quoted someone who said :
    >> Why is this method available when I haven't instantiated an object?


    Roedy Green wrote:
    > All static methods are available without an object. That is the whole
    > point of static methods.
    >
    > see http://mindprod.com/jgloss/static.html


    There is also the source,
    <http://download.oracle.com/javase/tutorial/index.html>

    --
    Lew
     
    Lew, Nov 19, 2010
    #7
  8. Mayeul <> writes:
    > On 18/11/2010 18:25, gary wrote:


    >> 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?


    > 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.

    --
    Jukka Lahtinen
     
    Jukka Lahtinen, Nov 19, 2010
    #8
  9. gary

    Roedy Green Guest

    On Fri, 19 Nov 2010 00:46:33 -0500, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    >There is also the source,
    ><http://download.oracle.com/javase/tutorial/index.html>


    which tutorial covers static vs instance?
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
     
    Roedy Green, Nov 19, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,987
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    542
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Purush
    Replies:
    4
    Views:
    1,766
    Purush Rudrakshala
    Apr 13, 2005
  4. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  5. Johannes Zellner
    Replies:
    22
    Views:
    3,762
Loading...

Share This Page