Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2004, 04:09 PM   PM User | #1
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
Java compiling issues

I'm sure this is really simple, but as I've just started coding Java, really, I can't figure out why this is happening. First, the code.

BusinessObject.java
PHP Code:
public abstract class BusinessObject {
    public 
Vector validationMessage;
    protected 
HashMap data = new HashMap();
    public 
HashMap read () {
       return 
data;
    }

Person.java
PHP Code:
public abstract class Person extends BusinessObject {
    public 
Person() {
    }
    public 
Person (String nameString) {
        
String newLast "";
        
String newFirst "";
        if ( 
nameString.indexOf(",") != -) {
            
newLast nameString.substring0nameString.indexOf(",") );
            
newFirst nameString.substringnameString.indexOf(",") + 1nameString.length() );
        } else if ( 
nameString.indexOf(" " ) != -) {
            
newFirst nameString.substring0nameString.indexOf(" ") );
            
newLast nameString.substringnameString.indexOf(" ") + 1nameString.length() );
        }
        
Person(newLastnewFirst);
    }
    public 
Person (String newNameLastString newNameFirst) {
        
data.put("nameLast"newNameLast);
        
data.put("nameFirst"newNameFirst);
    }

When I try to compile Person.java, the compiler tells me that at the line Person(newLast, newFirst); inside the Person (String nameString) constructor, it 'cannot resolve symbol' on Person (java.lang.String, java.lang.String). I've moved the last Person constructor above the others, between the others, etc., but I know that shouldn't make a difference. I've tried casting newLast = (String) nameString.substring(... and THAT doesn't work.

What am I doing wrong here?
Choopernickel is offline   Reply With Quote
Old 04-06-2004, 07:56 PM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
Change the line to:
Code:
this(newLast, newFirst);
In cases you'll want to call the superclass's constructor it's super(...).

shmoove

Edit:
The super(...) comment is irrelevant in your case, just for future reference.
shmoove is offline   Reply With Quote
Old 04-06-2004, 08:09 PM   PM User | #3
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
I tried this(newLast, newFirst) before I posted, actually, and a different compiler error happens: "call to this must be first statement in constructor."

I've thought of replicating the actual data.put() calls into the Person(nameString) constructor, but at that point my understanding of the flexibility and convenience of overloaded methods breaks down sobbing and runs away into its stash of Xanax.
Choopernickel is offline   Reply With Quote
Old 04-06-2004, 08:36 PM   PM User | #4
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
(double-posting, not editing, because of the time delay and the content)

I've found a solution, but it's not one I like, because it's not very legible,friendly, or efficient. Anybody got anything better?

PHP Code:
public Person (String nameString) {
    
this(
        
nameString.indexOf(",") != -
            
nameString.substring0nameString.indexOf(",") ) :
            
nameString.substring(nameString.indexOf(" ") + 1nameString.length() )
        ,
        
nameString.indexOf(",") != -
            
nameString.substring(nameString.indexOf(", ") + 1nameString.length() ):
            
nameString.substring0nameString.indexOf(" ") )
    );

I don't like using the same ternary operation twice in the code. I don't like stretching argument definitions out like that. I don't like being so damned confusing that even clear indentation doesn't help the legibility.

Grr.
Choopernickel is offline   Reply With Quote
Old 04-06-2004, 10:30 PM   PM User | #5
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
Or you could do this:
Code:
private void construct(String newNameLast, String newNameFirst) { 
  data.put("nameLast", newNameLast); 
  data.put("nameFirst", newNameFirst); 
}
public Person (String nameString) { 
  String newLast = ""; 
  String newFirst = ""; 
  if ( nameString.indexOf(",") != -1 ) { 
    newLast = nameString.substring( 0, nameString.indexOf(",") ); 
    newFirst = nameString.substring( nameString.indexOf(",") + 1, nameString.length() ); 
  } else if ( nameString.indexOf(" " ) != -1 ) { 
    newFirst = nameString.substring( 0, nameString.indexOf(" ") ); 
    newLast = nameString.substring( nameString.indexOf(" ") + 1, nameString.length() ); 
  } 
  construct(newLast, newFirst); 
}
public Person (String newNameLast, String newNameFirst) {
  construct(newNameLast,newNameFirst);
}
shmoove
shmoove is offline   Reply With Quote
Old 04-06-2004, 10:35 PM   PM User | #6
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
Does that follow any recommended practices? It looks decent, but it seems to me that having a non-constructor method do the work of a constructor method would be frowned-upon. True, it works. True, I don't know a lot yet. True, I'm picking nits.

Thanks for the help.
Choopernickel is offline   Reply With Quote
Old 04-06-2004, 11:55 PM   PM User | #7
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
I don't know about any recommended practices, just going by common sense.
But I think that by making that method private you couldn't be violating too many recomendations. It's just a helper function that has some reusable code. There's no rule (or recommendation) against calling methods from the constuctor as far as I know. And since it's private it has no effect on the interfaces you expose, so it's effect on the architecture of the application is non-existant.

shmoove

Edit:
PS: If it helps you sleep better you can rename the method to something other than "construct". Call it "createData" or something. ;P

Last edited by shmoove; 04-06-2004 at 11:57 PM..
shmoove is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:13 PM.


Advertisement
Log in to turn off these ads.