It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened,
visit the help center.
Closed 7 years ago.
Sorry, I know this answer is obvious but I suppose I'm just slow. Can anyone give me clear definitions of an instance variable and a constructor?
Instance variables:
Your class will need an instance variable named numbers that is a one-dimensional array. The size of the array will be determined by the constructor. You may, or may not, find it useful to have other instance variables.
Constructors:
You will write two constructors.
One constructor will be a default constructor that has no arguments. It will create an array that holds 10 int's and will set each element of the array to be 42.
The second constructor will take one argument, which will be an array of int. This constructor will create an instance array of the same size as the argument and then copy the integers from the argument to the instance array.
I have no idea how to even start on that.
Instance members are simply variables that belong to a class object, as long as it is not a static variable! A static variable, strictly speaking belongs to the class not an object,
Constructors is just a special method you call to create and initialize an object. It is also the name of your class.
So what you want is
class WhateverYourClassIs
{
int[] members;
public WhateverYourClassIs()//constructor.
{
members = new int[10] ;
}
public WhateverYourClassIs(int n) //overloaded constructor.
{
members = new int[n] ;
}
}
So as you can see in the above example, constructors like methods, can be overloaded. By overloaded it means the signature is different.
One constructor takes no argument, another takes a single argument that is an int.
A constructor is the part of a class which generates instances of that class. It's named the same thing as the class and has no return type. For example:
public class Foo{
public Foo(){
System.out.println("Hi from the constructor!!");
}
}
An instance field is a variable local to each instance of the class. You can either have public, protected, or private instance fields. A private instance field is "hidden" from the outside world and only the instance itself can access it. A public one is accessed using the .
operator. For example:
public class Foo{
public int x;
private int y;
}
Foo foo = new Foo(); //Thats a call to the constructor Foo()
foo.x = 1;
foo.y; //Error can't access private variables from outside the class
For your case, you'll want
class Name{
int[] numbers;
public Name(){
numbers = new int[10];
}
public Name(int n){
numbers = new int[n];
}
}
Here you overload the constructors (just like for methods) and create an array, which is a list of, in this case, int
s of a fixed length.
public class MyClass{
int numbers[];
MyClass(){
numbers = new int[10];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = 42;
}
}
MyClass(int[] array){
numbers = new int[array.length];
for (int i = 0; i < array.length; i++) {
numbers[i] = array[i];
}
}
}