Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Object as Reference not working

  1. #1
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Exclamation Object as Reference not working

    public class GG{
     
    	public static void main(String[] args) {
    		Integer i = new Integer(10);
    		System.out.println("Before Call:"+i);
    		change(i);
    		System.out.println("After Call:"+i);
     
    	}
     
    	public static void change(Integer x){
    		x=20;
    	}
    }
    Here the output is coming
    Before Call:10
    After Call:10


    I am confused when i am passing an object in a function then why the value is not changed as objects are passed by reference??
    Last edited by helloworld922; March 9th, 2010 at 10:33 AM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Object as Reference not working

    Double posted and answered on the Oracle forums.
    New To Java - Object as Reference not working..Please help..!!! [Locked]

    db

  3. #3
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: Object as Reference not working

    HI..Can u please tell me the link from where i can see a solution on oracle forums

    Thnks

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Object as Reference not working

    The link is right there in my previous response. Or log in to the Oracle forums and view your profile, where you will find a list of your posts.

    db

  5. #5
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Object as Reference not working

    Dont know if this has been answered or not, but you are using local variables, and expecting a global change. You have to either make i global, or change within the main method

Similar Threads

  1. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM
  2. Working with MAC .DMG files
    By mdv2000 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 21st, 2010, 12:31 PM
  3. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  4. Fitting a large primitive into a small reference variable
    By Phobia in forum Java Theory & Questions
    Replies: 15
    Last Post: October 23rd, 2009, 03:10 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM