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 3 of 3

Thread: Reference type

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reference type

    class Person {
     
        int value;
     
        Person() {
            value=0;
        }
     
     
    }
     
     
    class HelloWorld {
     
        public static void main(String[] args) {
            System.out.println("Hello World");
     
     
            Person p=new Person();
            System.out.println(p.value);
     
            //setValue(p,100);
            HelloWorld h=new HelloWorld();
            h.setValue(p,100);
     
            System.out.println(p.value);
     
     
        }
     
        static void setValue(Person p, int v) {
     
            p.value=v;
     
        }
    }

    p.value has changed from 0 to 100.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reference type

    Did you have a question?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Reference type

    Quote Originally Posted by gunitinug View Post
    p.value has changed from 0 to 100.
    Yes. Was this your question or doubt?
    setValue receives a copy of the reference to the Person object instantiated in main. This means that setValue cannot change the value of the 'p' variable in the main ..... but can change the state of the object, if it has accessible instance variables or any "mutator" methods.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. primitive vs reference data-type
    By Wolfone in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 30th, 2013, 06:26 AM
  2. Get Reference of js files
    By Priyankati in forum Other Programming Languages
    Replies: 1
    Last Post: February 11th, 2013, 12:04 PM
  3. Value vs Reference Types
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 23rd, 2012, 01:21 PM
  4. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  5. Object Reference
    By Mr.777 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 13th, 2011, 03:03 AM