0

I'm new to Java and having some problems, sorry if this is an obvious or often answered question, but I've looked at ~10 other errors of this type and couldn't figure out my problem. I'm getting errors in my main on the marked lines, saying that the constructor is undefined. Any help is appreciated.

public class ValueChanger {

    private static int myInt1;
    private static int myInt2;
    private static int myInt3;
    private static int myIntSum;

    public static void main(String[] args)
    {
        ValueChanger constructor = new ValueChanger(); //error here
        ValueChanger changeValues = new ValueChanger(); //and here
        changeValues.changingValues(myInt1, myInt2, myInt3, myIntSum);
    }

    public ValueChanger(int myInt1, int myInt2, int myInt3, int myIntSum)
    {
        myInt1 = 1;
        myInt2 = 2;
        myInt3 = 3;
        myIntSum = myInt1 + myInt2 + myInt3;
    }

    public int changingValues(int myInt1, int myInt2, int myInt3, int myIntSum)
    {
        myInt1 = myInt2;
        myInt2 = myInt3;
        myInt3 = myInt1;
        myIntSum = myInt1 + myInt2 + myInt3;
        return myIntSum;
    }

}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
John P.
  • 11
  • 3
    You haven't defined a constructor that takes zero arguments. You only defined one constructor, and it takes four integers. – Carcigenicate Jun 25 '17 at 17:57
  • You should really find out what `static` means... (with regard to the `myInt1` etc variables). Also, you're not setting the class variables in the constructor, you're just changing the values of the parameters. – Andy Turner Jun 25 '17 at 17:59
  • Also note this class doesn't actually appear to do anything. Your variable swapping is broken, and order doesn't matter when summing, so myIntSum won't change. Also, why give the constructor myIntSum if you may ignore it any overwrite it? – Carcigenicate Jun 25 '17 at 18:00
  • Default Constructors are defined implicitly only when no other constructors are defined. – GAURANG VYAS Jun 25 '17 at 18:02
  • @AndyTurner - I set those as static because of a message on `changeValues.changingValues(myInt1, myInt2, myInt3, myIntSum);` that said "Cannot make a static reference to the non-static field myInt1". That said, you're right that I don't actually know what it means, sorry, I'm very new to this. Also, would you mind explaining how to actually set the variables? Should I set them in the default constructor instead? – John P. Jun 25 '17 at 18:32

4 Answers4

1

When you implement a constructor explicitly, default constructor will remove. So your constructor expect four args but when you create instances you haven't provide any args:

ValueChanger changeValues = new ValueChanger();

You need to implement overloaded constructor for this.

As a example:

public ValueChanger()
{
    myInt1 = 0;
    myInt2 = 0;
    myInt3 = 0;
    myIntSum = myInt1 + myInt2 + myInt3;
}

Now you can instantiate objects as:

ValueChanger changeValues = new ValueChanger();

Or without implementing default constructor, you can pass values when instantiating objects.

Like:

ValueChanger constructor = new ValueChanger(3, 4, 4, 11); //add args
ValueChanger changeValues = new ValueChanger(4, 5, 6, 15);

Read about overloaded constructor.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
1

Read this carefully:

  1. If you do not write any constructor in a class, a default no-argument constructor is provided by compiler.
  2. When you do not explicitly write a no-argument constructor for a class, the compiler won't complain as long as objects are built without parameter constructors. Because compiler allows default constructor to create objects, which itself is a no-argument constructor.
  3. If you write only constructors which takes arguments and do not define no-argument constructor, default constructor is not provided by compiler and in this case if you try to create object using no-argument constructor, it looks in your class for a no-argument constructor. That is not present in your case. And so you will get an error saying that default constructor is not available. And it is the case if you want to block creation of objects without any data in it, this is a good approach.

Example: consider that an student object must have a roll-no associated to it. For achieving this, define a single argument constructor and don't define no-argument constructor.

Solution to your error: Add default constructor-

public ValueChanger()
{
}

For more detail see here

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28
0

Since you have defined your own constructor, you cannot use the default constructor(the one without any parameters).

You may want to add the default constructor also:

public ValueChanger(){

}

or use ValueChanger valueChanger = new ValueChanger(1,2,3,4);

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • I've added the default constructor now, but there is an error on: `ValueChanger constructor = new ValueChanger();` - "The value of the local variable constructor is not used" – John P. Jun 25 '17 at 18:03
  • @JohnP. That is not error. That is warning. Because you created the variable, but never used later. – Nabin Bhandari Jun 25 '17 at 18:06
-1

Add this to your class:

public ValueChanger()
{
}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204