HardwareHeaven.com

HardwareHeaven.com

Looking for the skin chooser?
 
 
  • Home

  • Hardware reviews

  • Articles

  • News

  • Tools

  • Gaming at HardwareHeaven

  • Forums

 

Go Back   HardwareHeaven.com > Forums > Software / Tools > Programming, Coding, (Web)Design


Programming, Coding, (Web)Design Discuss all your programming or design needs with likeminded people.

Reply
 
Thread Tools
Old Oct 15, 2003, 03:45 PM   #1
DivrerHaeven Seinor Mebmer
 
Join Date: Mar 2003
Location: Baltimore, MD
Posts: 236
Rep Power: 0
Oblivious is on a distinguished road

??? Overloading constructors in Java?

I was under the impression that it was possible to overload constructors in Java. Or maybe there is something wrong with my syntax? Can you help me out here guys and gals - whats wrong with this code?

-------------------------------------------------
Code:
public class Ip_WOccur {
    public char[] ip;
    public int instances;
    /** Creates a new instance of Ip_WOccur */
    public Ip_WOccur( char[] init_ip, int init_instances ){
        instances = init_instances;
        ip = new char[init_ip.length];
        for( int some_var=0 ; some_var<ip.length ; some_var++ ){
            ip[some_var] = init_ip[some_var];
        }
    }
    public Ip_WOccur( char[] init_ip ) {
        this.Ip_WOccur(init_ip, 0);
    }
}
------------------------------------------------------
Code:
[25:1] cannot resolve symbol
symbol  : method Ip_WOccur (char[],int)
location: class Apache.Apache_Companion.Ip_WOccur
        this.Ip_WOccur(init_ip, 0);
        ^
1 error
-------------------------------------------------------


As you can probably tell, Java is new to me so I would greatly appreciate the efforts of anyone who can share their wisdom with me.

P.S. - I am using Sun Java 1.4.2 VM
__________________
Pepole olny raed wrods. Btu oyu shuodl sitll check oyur spleling!
http://realgar.hopto.org
Oblivious is offline   Reply With Quote


Old Feb 21, 2004, 10:08 PM   #2
DriverHeaven Newbie
 
Join Date: May 2002
Location: Ireland
Posts: 16
Rep Power: 0
baz8080 is on a distinguished road

this thread is old , and you've probably since fixed it yourself, but dump the 'this.' in front of the 'Ip_WOccur(init_ip, 0);'

'this' is used to reference instance variables. Your instance variables should really also be private, no public.
baz8080 is offline   Reply With Quote
Old Feb 23, 2004, 12:13 PM   #3
A Legend in Underwear
 
UberLord's Avatar
 
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0
UberLord will become famous soon enough

Kinda odd.

this should reference the entire object, including it's private stuff. At least, thats the behaviour under C# and a few other langs - although I really don't know Java
__________________
Gentoo Linux - Developer (baselayout)
Read my blog

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours."
Stephen Roberts
UberLord is offline   Reply With Quote
Old Feb 23, 2004, 12:27 PM   #4
Back in London
 
germanjulian's Avatar
 
Join Date: Jul 2003
Location: London
Posts: 1,797
Rep Power: 0
germanjulian is on a distinguished road

Donator
java is very different to C++ and C but has the obvious things which are the same for nearly all programming languages. why are you doing java? cause simply put java is rubbish. avoide like the plague if you can use C or php
__________________
/|\ Asus P5W DH Deluxe, Intel C2D E6600, 2GB Corsair XMS2-6400C4 DDR2, E-VGA GeForce 7800 GT, Creative X-Fi Extreme Music, 500GB Seagate 7200.10 SATA, Lian Li PC-V1100 Aluminum Case Black, etc. http://germanjulian.com /|\
germanjulian is offline   Reply With Quote
Old Feb 23, 2004, 01:49 PM   #5
A Legend in Underwear
 
UberLord's Avatar
 
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0
UberLord will become famous soon enough

Quote:
Originally posted by germanjulian
cause simply put java is rubbish
How many languages do you know of that work on PC's, big iron mainframes, pda's and mobile phones?

I only know of one - java. So no, it's not rubbish
__________________
Gentoo Linux - Developer (baselayout)
Read my blog

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours."
Stephen Roberts
UberLord is offline   Reply With Quote
Old Feb 23, 2004, 04:07 PM   #6
DriverHeaven Newbie
 
Join Date: May 2002
Location: Ireland
Posts: 16
Rep Power: 0
baz8080 is on a distinguished road

I'd be with Uberlord on this one. Java can't be put down as rubbish. It's a 3.5Gl, C is 3, they both have their place. C turns to machinecode, Java to bytecode etc.

The guy is calling a constructor from a constructor, whereas he should have 2 proper constructers. 'this.' is also being used incorrectly here.

It also looks like some of the processing is happening in the first constructor, which is also not advisable. The instance variables are public also, so proper OOP is out the window.

I'm sure he's just learning and has since solved all of this.
baz8080 is offline   Reply With Quote
Old May 31, 2004, 09:48 AM   #7
Blues man
 
Join Date: Jul 2003
Location: In front of my computer
Posts: 35
Rep Power: 0
Eman Resu is on a distinguished road

public Ip_WOccur( char[] init_ip ) {
this(init_ip, 0); <- this is the way it should be done
}
Eman Resu is offline   Reply With Quote
Reply

Thread Tools