Confusing Dot Operator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Confusing Dot Operator

Hybrid View

  1. #1
    Join Date
    Sep 2012
    Posts
    10

    Confusing Dot Operator

    I am learning Java and I have noticed when setting an image to an icon they use: Icon img1 = new ImageIcon(getClass().getResource("YES.png"));
    getClass() method followed by a dot and another method next to it? If they were arguments to the ImageIcon constructor, shouldn't they be separated by commas? What does this mean?
    In another topic, setLayout(new FlowLayout()); for example, uses the new operator inside a method without the object reference variable such as button = new button(); Why are they only using the new keyword without the object reference? How is this possible? Thank you all in advance for the responses, they are very appreciated.

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Confusing Dot Operator

    The dot notation and the new operator are very basic Java. Check them out in a textbook or tutorial.

  3. #3
    Join Date
    Sep 2012
    Posts
    10

    Re: Confusing Dot Operator

    Why I am here if I could do that? If you're not going to answer my questions don't answer at all, just go away.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Confusing Dot Operator

    I am assuming you already know what the dot notation means when used with an object reference and are just confused about using it with method call. If not do as nuzzle has suggested and read a basic tutorial.

    Basically what you have shown a just a shorthand way of writing code. Rather than assign the reference returned from getClass() to a local variable and then using that local variable to call the object getResource(..) method you can write in as you have shown. In fact you can chain as many method calls as you like provide each method call returns an appropriate object reference for the following call. Back to your example, the following code snippets are essentially the same:

    Code:
    Icon img1 = new ImageIcon(getClass().getResource("YES.png"));
    Code:
    Class c = getClass();
    URL url = c.getResource("YES.png");
    Icon img1 = new ImageIcon(url);
    The second example you have shown is also a form of shorthand similar to the example above - I'll leave you to work that one out.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Sep 2012
    Posts
    10

    Re: Confusing Dot Operator

    Thank you. So instead of using the reference returned by the method; the method itself is called, I understand now, the other question about the new operator still hangs for me. Your response is appreciated double since you actually answered it and not told me to basically "Google it" as the post above.
    Last edited by Fanatic2012; November 22nd, 2012 at 02:37 PM.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Confusing Dot Operator

    Quote Originally Posted by Fanatic2012 View Post
    Your response is appreciated double since you actually answered it and not told me to basically "Google it" as the post above.
    You're wellcome. I knew you would appreciate my advice to read a book.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Confusing Dot Operator

    the other question about the new operator still hangs for me.
    It's exactly the same thing, the setLayout(..) method calls requires you pass a reference to a LayoutManager. new FlowLayout() creates an instance of FlowLayout and returns a reference to the created object. Now work the rest out yourself - it's exactly the same as the other issue you had.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center