Solved

easy java question, constructor not defined

Posted on 2006-10-29
10
209 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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Accepted Solution

by:
suprapto45 earned 500 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

803 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