Results 1 to 10 of 10
  1. #1
    gregoost is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Aliasing in methods

    hello guyz, Im quite new to Java and still studying and getting used to it. I want to know if there is a way to avoid aliasing of method calls in java. for an example take following code.



    class Letter {
    char c;
    }
    public class PassObject {
    static void f(Letter y) {
    y.c = z;
    }
    public static void main(String[] args) {
    Letter x = new Letter();
    x.c = a;
    print("1: x.c: " + x.c);
    f(x);
    print("2: x.c: " + x.c);
    }

    (this is not exactly a correct java code but I hope u get the idea). I am supposed to modify this code to address aliasing. I'll be really grateful if anyone could help me on this.thanx..

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Aliasing in methods


  3. #3
    gregoost is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Re: Aliasing in methods

    Thanx..but the problem is that there is no reply for him also because he has been told that he is cross posting my post. So please, is there no slution??

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Aliasing in methods

    Can you define what you mean by aliasing?
    You code has no comments so I have no idea what point you are trying to make or what the problem is.

  5. #5
    gregoost is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Re: Aliasing in methods

    well thats actually a problem for me too. When I searched in the internet about aliasing in method calls, I gt a rough idea that aliasing is something that happens due to 'pass by refference' nature of Java. Above question is given to us by our professor and all he said was to 'modify the above code in order to address aliasing'. Neither do I have a clear idea of what is to be done. I was hoping maybe one of you guys may know what it means..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Aliasing in methods

    Do you have any explanation of what the purpose of the posted code is?
    What happens if you clean it up and execute it?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Aliasing in methods

    I gt a rough idea that aliasing is something that happens due to 'pass by refference' nature of Java.
    Wrong idea. Java does NOT have any 'pass by reference'

    Primitives are passed by value. References are passed by value. Objects aren't passed at all.

    db
    Last edited by DarrylBurke; 09-06-2011 at 05:14 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Aliasing in methods

    You don't wanna know what I passed this morning.

  9. #9
    gregoost is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Re: Aliasing in methods

    uh huh...okay...thanx for the help everyone..

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Aliasing in methods

    The only thing I can come up with is this:

    Java Code:
    public class MyClass {
       private MyOtherClass moc= new MyOtherClass();
       ...
       public aMethod() { aliasMethod(moc); }
    
       public void aliasMethod(MyOtherClass m) {
          m.someMethod();    // m is an alias for moc
          moc.someMethod();
       }
    }
    But I don't see any problems with it; except for the compiler that can't do certain optimizations ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Methods Help
    By uppercool in forum New To Java
    Replies: 8
    Last Post: 04-12-2011, 01:46 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. Aliasing In 2d Arraylist
    By hatela150 in forum Java 2D
    Replies: 1
    Last Post: 12-11-2008, 06:01 AM
  5. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 10:00 AM

Posting Permissions

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