Solved

easy java question, constructor not defined

Posted on 2006-10-29
10
206 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
Receive file in Servlet 1 36
How Complex Is This Java Course ? 9 63
github account with ecipse 1 16
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now