Solved

easy java question, constructor not defined

Posted on 2006-10-29
10
215 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.
Suggested Courses

615 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