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

Thread: Iinitialization of members. From Qt (C++) to Java

  1. #1
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Iinitialization of members. From Qt (C++) to Java

    Hi,

    Please, help me to write analog of this code in Java:

    #include <QString>
     
    class Person
    {
    public:
        Person( QString nickName = QString( "" ),
                QString firstName = QString( "" ),
                QString lastName = QString( "" ),
                QString phoneNumber = QString( "" ),
                QString email = QString( "" )) :
            m_nickName( nickName ),
            m_firstName( firstName ),
            m_lastName( lastName ),
            m_phoneNumber( phoneNumber ),
            m_email( email )
        {
     
        }
     
        // ...
    }


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    A good start would be to read up on the differences between
    C++ and Java. I'm guessing you made this program using
    the Qt Creator IDE (not a fan - but anyway it's not as simple
    as changing one or two phrases. Main is defined differently in
    Java as is classes and instances, fields etc.

    Once you understand the basic syntactical differences it's not
    too hard to accomplish.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    QtFalcon (August 3rd, 2014)

  4. #3
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    Please, see my code:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package my.checkpersondata;
     
    /**
     *
     * @author Ivan
     */
    public class Person
    {
        Person( String nickName,
                String firstName,
                String lastName,
                String phoneNumber,
                String email)
        {
            m_nickName = nickName;
            m_firstName = firstName;
            m_lastName = lastName;
            m_phoneNumber = phoneNumber;
            m_email = email;
        }
     
        public void setNickName( String nickName )
        {
            m_nickName = nickName;
        }
     
        public void setFirstName( String firstName )
        {
            m_firstName = firstName;
        }
     
        public void setLastName( String lastName )
        {
            m_lastName = lastName;
        }
     
        public void setPhoneNumber( String phoneNumber )
        {
            m_phoneNumber = phoneNumber;
        }
     
        public void setEmail( String email )
        {
            m_email = email;
        }
     
        public String getNickName()
        {
            return m_nickName;
        }
     
        public String getFirstName()
        {
            return m_firstName;
        }
     
        public String getLastName()
        {
            return m_lastName;
        }
     
        public String getPhoneNumber()
        {
            return m_phoneNumber;
        }
     
        public String getEmail()
        {
            return m_email;
        }
     
        private String m_nickName;
        private String m_firstName;
        private String m_lastName;
        private String m_phoneNumber;
        private String m_email;
    }

  5. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    Do you have any problems with that code?

  6. The Following User Says Thank You to Cornix For This Useful Post:

    QtFalcon (August 3rd, 2014)

  7. #5
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    No

    How to set default values in constructor how in Qt:

    class Person
    {
    public:
        Person( QString nickName = QString( "" ),
                QString firstName = QString( "" ),
                QString lastName = QString( "" ),
                QString phoneNumber = QString( "" ),
                QString email = QString( "" )) :
            m_nickName( nickName ),
            m_firstName( firstName ),
            m_lastName( lastName ),
            m_phoneNumber( phoneNumber ),
            m_email( email )
        {
     
        }
     
        // ...
    }

  8. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    You can pass default values to fields via the constructor
    parameter. Which in turn would call the 'set' method of
    each field to initialize it.

    public class Example {
       public Example(int x, int y) {
          setMethodX(x);
          setMethodY(y);
       }
    }

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  9. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    QtFalcon (August 3rd, 2014)

  10. #7
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    What's the difference between:
    public class Example {
       public Example(int x, int y) {
          setMethodX(x);
          setMethodY(y);
       }
    }

    And:
    public class Example {
       public Example(int x, int y) {
          m_x = x;
          m_y = y;
       }
       private String m_x;
       private String m_y;

    I want to use Person-object like this:
    Person person = new Person();


    --- Update ---

    I could use this object in Qt like this:
    Person person;
    person.setFirstName( "Ivan" );

    I will use this object in Java like this:
            String nickName = "";
            String firstName = "";
            String lastName = "";
            String phoneNumber = "";
            String email = "";
     
            Person person = new Person( nickName, firstName, lastName,
            phoneNumber, email );

    Thanks for everyone!

  11. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    You can overload constructors the same way that you can overload any method in java.
    public class MyClass {
     
            public MyClass (int i) {
                    // Do stuff
            }
     
            public MyClass () {
                    this(0); // Default value for i is 0.
            }
     
    }

  12. The Following User Says Thank You to Cornix For This Useful Post:

    QtFalcon (August 3rd, 2014)

  13. #9
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    Thanks! It is how in C++

    public class Person
    {
        Person( String nickName,
                String firstName,
                String lastName,
                String phoneNumber,
                String email)
        {
            m_nickName = nickName;
            m_firstName = firstName;
            m_lastName = lastName;
            m_phoneNumber = phoneNumber;
            m_email = email;
        }
     
        Person( ) {
            m_nickName = "";
            m_firstName = "";
            m_lastName = "";
            m_phoneNumber = "";
            m_email = "";
        }
        // ...

    I don't understand this line:
    this(0); // Default value for i is 0.

    Now I can write instead this code:
        /**
         * Test of setNickName method, of class Person.
         */
        @Test
        public void testSetNickName()
        {
            String nickName = "";
            String firstName = "";
            String lastName = "";
            String phoneNumber = "";
            String email = "";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
            person.setNickName( "8Observer8" );
     
            String actual = person.getNickName();
            String expected = "8Observer8";
     
            assertEquals( expected, actual );
        }

    This code:
        /**
         * Test of setNickName method, of class Person.
         */
        @Test
        public void testSetNickName()
        {
            Person person = new Person( );
            person.setNickName( "8Observer8" );
     
            String actual = person.getNickName();
            String expected = "8Observer8";
     
            assertEquals( expected, actual );
        }

  14. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    The "this" keyword in the constructor is used to call another constructor.
    It is usually a better idea to have only a single constructor that initializes member variables, all other constructors call this master constructor and just pass the default values as arguments.
    For your very simple example this is not needed, but its just a better style and much safer to have a single point of failure.

  15. The Following User Says Thank You to Cornix For This Useful Post:

    QtFalcon (August 3rd, 2014)

  16. #11
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    This line doesn't work:
    this( "", "", "", "", "" );

    /**
     *
     * @author Ivan
     */
    public class Person
    {
        public Person( String nickName,
                String firstName,
                String lastName,
                String phoneNumber,
                String email)
        {
            m_nickName = nickName;
            m_firstName = firstName;
            m_lastName = lastName;
            m_phoneNumber = phoneNumber;
            m_email = email;
        }
     
        public Person( ) {
            this( "", "", "", "", "" );
        }
     
        // ...
     
    }

  17. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    We don't have crystal balls. How does it not work?
    Copy and paste full error messages.
    Improving the world one idiot at a time!

  18. The Following User Says Thank You to Junky For This Useful Post:

    QtFalcon (August 3rd, 2014)

  19. #13
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Iinitialization of members. From Qt (C++) to Java

    Sorry! There are no mistakes

Similar Threads

  1. [code=java]Greetings to all members[/code]
    By Gurutech in forum Member Introductions
    Replies: 1
    Last Post: May 4th, 2014, 01:41 PM
  2. Hello Java Forums Members
    By AsifHassan in forum Member Introductions
    Replies: 1
    Last Post: May 24th, 2013, 01:56 PM
  3. Hello to all java community members
    By sihle in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2013, 08:16 AM
  4. Hi Members
    By abuwalad in forum Member Introductions
    Replies: 0
    Last Post: April 30th, 2012, 04:20 PM
  5. Hello to java forum members
    By gaikwaddeepali111 in forum Member Introductions
    Replies: 1
    Last Post: September 19th, 2011, 10:45 AM