• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OO concept order of processing question

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen this a lot in code and just don't understand the processing of this example:
Iterator Keys =wc.keyset().iterator();

I am confused with the second method call above. What is this doing? I understand the following:
Iterator Keys =wc.keyset();

Thanks in advance
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the context of your code, but it looks like wc is a Map of some kind. If you read the API, you'll see what keySet() returns, and then if you look up the API for the class of the object returned, you'll of course see that it has an iterator() method, which of course returns an iterator.

If the syntax confuses you, object.method1().method2() is valid when object.method1() returns an object with a method2() method. This easily generalizes. That is, think of wc.keySet() as an object of the type returned by the keySet() method for the object wc. Then iterator() returns an iterator on that object.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

  • You have a Map designated by wc.
  • You call keyset() on the Map in order to get a Set of the Map's keys. (So "wc.keyset()" designates a Set. Note that since keys must be unique, this constitutes a Set.)
  • You then call iterator() on the Set to get an Iterator object for the keys.
  • You assign this Iterator to a variable called "Keys" (which should be lowercase).
  •  
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    With other words,

    x.y().z();

    is the same as

    a = x.y();
    a.z();

    Does that help?
     
    tim mahoney
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks guys,
    Ilja Preuss could you please explain your example with this code. I still dont get the example.
    <%
    java.util.Enumeration e = System.getProperties().propertyNames();
    while( e.hasMoreElements() )
    {
    String prop = (String)e.nextElement();
    out.print(prop);
    out.print(" = ");
    out.print( System.getProperty(prop) );
    out.print("<br>");
    }

    %>
    Thanks much.

    [ April 27, 2005: Message edited by: tim mahoney ]

    [ April 27, 2005: Message edited by: tim mahoney ]
    [ April 27, 2005: Message edited by: tim mahoney ]
     
    Ilja Preuss
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Enumeration e = System.getProperties().propertyNames();

    You can think of this as System.getProperties() being assigned to a kind of "anonymous local variable", and then immediately calling the propertyNames method on that variable.

    that is, it is the same as

    Properties properties = System.getProperties();
    Enumeration e = properties.propertyNames();

    Does that help?
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you look up the System class in the API, you'll see that its getProperties() method returns a Properties object. So we could say...

    Properties p = System.getProperties();

    If you look up the Properties class in the API, you'll see that its propertyNames() method returns an Enumeration object. So we could say...

    Enumeration e = p.propertyNames();

    Therefore, we could use the above two lines of code to get our Enumeration object, or we could combine those lines into a single statement...

    Enumeration e = System.getProperties().propertyNames();



    1.4.2 API:
    http://java.sun.com/j2se/1.4.2/docs/api/index.html

    System:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html

    Properties:
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html
     
    tim mahoney
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right on guys. You are awesome. thanks
     
    tim mahoney
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey guys,

    I tried to implement the examples you gave me with my own code and I am running into some problems. I would like to accomplish the same thing as above with the code below. In the class 'example1' I am returning the example2. I must be doing something wrong here , because I cant get it to work. Any help would be appreciated. Thanks much.

    --------------------
    public class HelloWorldApp {
    public example2 v_example;
    public static void main(String[] args) {
    System.out.println("BEGIN CODE");
    //example2 v_example=example1().example2();
    example2 v_example= new example1();
    System.out.println("END CODE");
    }
    }
    -----------------------
    public class example1
    {
    public example2 example1(){
    System.out.println("PRINTINT EXAMPLE 1");
    example2 showthis=new example2();
    return showthis;
    //return showthis;
    ------------------------
    public class example2
    {
    public example2(){
    System.out.println("example 2!");
    }
    }
     
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What do you mean that "you can't get it to work?" You need to provide more details in order for us to help you fix the problems. Does the above code compile? If not, what error messages do you get? If it does compile, what happens when you run it and how does it differ from what you expect?

    Layne
     
    tim mahoney
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I get an invalid datatype in the HelloWorldApp. I am trying to recreate my own classes in order to emulate the example above. In the example code I am returning an object example 2.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    First, you can define a class within another class, but you can't define a class within a method (or constructor). So if you want the definition for Example2 to be nested inside Example1, then you'll need to move the Example2 definition outside of Example1's constructor.

    Next, in the context of HelloWorldApp, the type "Example2" has no meaning. Instead, it needs to be "Example1.Example2" because Example2 is an inner class of Example1.

    Next, constructors don't have return types. So "public Example2 Example1() {..." is not a constructor, and will not be called as part of creating an Example1 object. Instead, you should write a method to specifically return an instance of Example2, and then call that method.
     
    Ranch Hand
    Posts: 580
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by marc weber:
    First, you can define a class within another class, but you can't define a class within a method (or constructor). So if you want the definition for Example2 to be nested inside Example1, then you'll need to move the Example2 definition outside of Example1's constructor.

    Next, in the context of HelloWorldApp, the type "Example2" has no meaning. Instead, it needs to be "Example1.Example2" because Example2 is an inner class of Example1.

    Next, constructors don't have return types. So "public Example2 Example1() {..." is not a constructor, and will not be called as part of creating an Example1 object. Instead, you should write a method to specifically return an instance of Example2, and then call that method.




    You can use anonymous inner classes if you wish within the body of a method/constructor.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by James Carman:
    ... You can use anonymous inner classes if you wish within the body of a method/constructor.


    Good point! I hadn't thought of that. (Although a bit advanced for this situation, I think...)
     
    tim mahoney
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Cool Thanks guys. Got it to work and makes sense. Being a procedural programmer, the OO stuff is quite a challenge for me. For anyone having some problems understanding what I went through. Here are my classes:
    ---------------------------------------------
    public class Test2{
    //
    int b=4;
    int a;
    Test2()
    {
    //System.out.println("Test Called");
    a=5;
    b=5+1;
    }

    }
    --------------------------------------------------------
    public class Test1{
    //
    int b=4;
    int a;
    Test1(int i)
    {
    //System.out.println("Test Called");
    a=i;
    b=i+1;
    }
    Test1 incrByTen()
    {
    b=55;
    System.out.println("im here");
    Test1 temp =new Test1(a+10);
    return temp;
    }
    Test2 getval(){
    Test2 temp2= new Test2();
    return temp2;
    }
    }
    -----------------------------------------------------

    public class RetOb
    {
    public static void main(String args[])
    {
    //Test1 ob1= new Test1(2);
    //System.out.println("begin ob1.a: "+ ob1.a);
    //Test1 ob2;
    Test1 ob2=new Test1(10).incrByTen();
    //ob2=ob1.incrByTen();
    //System.out.println("ob1.a: "+ ob1.a);
    System.out.println("ob2.a: "+ ob2.a);
    System.out.println("ob2.b: "+ ob2.b);
    ob2= ob2.incrByTen();
    System.out.println("after second increase: "+ ob2.a);

    Test2 z = new Test1(10).getval();
    System.out.println("---------------------");
    System.out.println("final "+ z.a);
    }

    //System.out.println("final");
    }
    -------------------------------------------------------
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by tim mahoney:
    ... Got it to work and makes sense...


    Excellent!
     
    reply
      Bookmark Topic Watch Topic
    • New Topic