Can't get constructor from different class within same package to be recognized.

StackOverflow https://stackoverflow.com/questions/9854068

  •  26-05-2021
  •  | 
  •  

Domanda

So these 2 files I am going to post are each in my DataBase package. The DBBinding class just needs to create an object with a string for the key and one for the value. Then the DBrecord is going to keep a collection of DBBindings that all have the same key string but diffrent value strings. For some reason I can not think/find the correct way to make a add method in DBrecord so that it calls the DBBinding class/objects constructor.

This is the method that is supposed to add the binding:

private void addRecord(String key_, String value_)
{
    //DBBinding myDBBinding=new DBBinding(key_, value_);//constructor not defined error
    //DBBinding myDBBinding(key_,value_);
    //DataBase.DBBinding myDBBinding=new DataBase.DBBinding(key_, value_);//constructor not defined error

}

Here's the DBBinding code followed by the DBrecord code.

package DataBase;

public class DBBinding {

    private String key;
    private String value;

    public void DBBinding(String key_, String value_)
    {
        String key =new String(key_);
        String value=new String(value_);
    }

    //public String toString()
    //{return key+": "+value;}

}

and

package DataBase;
//package DataBase.*;
import DataBase.*;//did not help ... ?

public class DBrecord {

    boolean select;
    String key;
    //need some type of collection to keep bindings. 

    public void DBrecord()
    {
        DBrecord myRecord=new DBrecord();
        select=false;
    }

    private void addRecord(String key_, String value_)
    {
        //DBBinding myDBBinding=new DBBinding(key_, value_);//constructor not defined error
        //DBBinding myDBBinding(key_,value_);
        //DataBase.DBBinding myDBBinding=new DataBase.DBBinding(key_, value_);//constructor not defined error

    }

    public String toString()
    {
        //out put key first then all values in collection/group/record. use correct formatting. 
    }



}
È stato utile?

Soluzione

In class DBBinding you must have public DBBinding(String key_, String value_), the void makes the "constructor" actually to a method :-)

You have the same error in DBrecord.

By the way, don't do this:

String key =new String(key_);

Strings are immutable, nothing can happen if you "share" them. But your code forces Java to create a new object for an absolutely identical value. So use just

String key = key_;

However, in your case even this is wrong, as you create a new local variable key "shadowing" the class variable key. Look here for an explanation.

So alltogether DBBinding should look like:

package DataBase;

public class DBBinding {

    private String key;
    private String value;

    public DBBinding(String key_, String value_)
    {
        key = key_;
        value =  value_;
    }
    ...
}

Altri suggerimenti

If you want your code to work please remove the void construct from your constructors.

Signature differences Constructors and methods differ in three aspects of the signature: modifiers, return type, and name. Like methods, constructors can have any of the access modifiers: public, protected, private, or none (often called package or friendly). Unlike methods, constructors can take only access modifiers. Therefore, constructors cannot be abstract, final, native, static, or synchronized.

The return types are very different too. Methods can have any valid return type, or no return type, in which case the return type is given as void. Constructors have no return type, not even void.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top