?
Solved

easy java question, constructor not defined

Posted on 2006-10-29
10
Medium Priority
?
217 Views
Last Modified: 2010-03-31
Hi everyone,

easy question coming from c++... my java compiler (net beans) says I have an error using the constructor of CPerson in the last line of main():

    public static void main(String[] args)
    {
        // My linked list container.
        CLinkedList list;

        // Add a node.
        list.AddNode(
            CPerson("Joe", "111-222-3333", "some street"));
    }

It looks like it's saying that constructor method is not defined, but declaring a CPerson like this:

     CPerson person;

is ok. This is how I defined CPerson though:

    public class CPerson {
   
        /** Creates a new instance of CPerson */
        public CPerson()
        {
        }
        public CPerson(String strName,
                       String strPhoneNumber,
                       String strAddress)
        {
            m_strName = strName;
            m_strPhoneNumber = strPhoneNumber;
            m_strAddress = strAddress;
        }
    }

I must be something really simple. Why can't I use the constructor like that?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Accepted Solution

by:
suprapto45 earned 2000 total points
ID: 17831751
>>"        public CPerson(String strName,
                       String strPhoneNumber,
                       String strAddress)
        {
            m_strName = strName;
            m_strPhoneNumber = strPhoneNumber;
            m_strAddress = strAddress;
        }"

You do not define any m_strName, m_strPhoneNumber and m_strAddress.

    public class CPerson {
        private String m_strName;
        private String m_strPhoneNumber;
        private String m_strAddress;

        /** Creates a new instance of CPerson */
        public CPerson()
        {
        }
        public CPerson(String strName,
                       String strPhoneNumber,
                       String strAddress)
        {
            m_strName = strName;
            m_strPhoneNumber = strPhoneNumber;
            m_strAddress = strAddress;
        }
    }


Then......

        // Add a node.
        list.AddNode(
            CPerson("Joe", "111-222-3333", "some street"));

Where is the keyword "new"?
For example,

CPerson test = new CPerson("Joe", "111-222-3333", "some street");
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17831760
Then,

You also need to define the getter and setter (I think that this is also present in C++) so you should know about it :)

David
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17832107
>>list.AddNode(CPerson("Joe", "111-222-3333", "some street"));


should be

list.AddNode(new CPerson("Joe", "111-222-3333", "some street"));
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 17832284
> should be
> list.AddNode(new CPerson("Joe", "111-222-3333", "some street"));


thats already been pointed out
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17832289
Missed it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17832290
Sorry suprapto45
0
 
LVL 92

Expert Comment

by:objects
ID: 17832291
you are meant to actually *read* all previous comments before posting :)
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17834023
sorry I just posted a code snippet, I do have all the member variables defined, it was just that I was not using the new operator.

Why do we need to use the new operator in java for this example? Is it not possible to create class objects on the stack like that?

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17834774
>>Is it not possible to create class objects on the stack like that?

No, Java objects are created using new on the heap. There are static classes though and classes with static methods

http://mindprod.com/jgloss/static.html
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17839620
Just be able to get online again.

Glad I could help :).

>>"Sorry suprapto45"
Not a problem at all. We all are just trying to help here.

David
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question