How do i access the object created inside constructor? [duplicate] - java

This question already has answers here:
What is the difference between field, variable, attribute, and property in Java POJOs?
(11 answers)
Closed 5 years ago.
I want to create an array for each object i create, but i cant get access to it. since its scope is within the constructor.
class Constructor{
Constructor(int vsl)
{
int[] array = new int[vsl];
}
}
If i call this constructor by Constructor c = new Constructor(4);
how can i use array in my code?
Note: i want to specifically create the object inside the constructor and manipulate it using values i get from scanner object.

You can not, that array is scoped and visible only inside of the constructor
what you have to do is declare that array as a member class and initialize it in the constructor:
class Constructor {
private int[] array;
Constructor(int vsl) {
array = new int[vsl];
}
}

Related

Method not storing data in object myRecipeBox [duplicate]

This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Closed 8 years ago.
I was reading a book and came across the term Shadow Variables in Java but there was no description for it. Eventually what are these variables used for and how are they implemented?
Instead of providing my own description i may ask you to read about it for example here: http://en.wikipedia.org/wiki/Variable_shadowing. Once you understood the shadowing of variables i recommend you proceed reading about overlaying/ shadowed methods and visibility in general to get a full understanding of such terms.
Actually since the question was asked in Terms of Java here is a mini-example:
public class Shadow {
private int myIntVar = 0;
public void shadowTheVar(){
// since it has the same name as above object instance field, it shadows above
// field inside this method
int myIntVar = 5;
// If we simply refer to 'myIntVar' the one of this method is found
// (shadowing a seond one with the same name)
System.out.println(myIntVar);
// If we want to refer to the shadowed myIntVar from this class we need to
// refer to it like this:
System.out.println(this.myIntVar);
}
public static void main(String[] args){
new Shadow().shadowTheVar();
}
}

What is the best way to set empty array for properties in class Java? [duplicate]

This question already has answers here:
Default constructor vs. inline field initialization
(5 answers)
Closed 3 years ago.
I have a class like:
public class TemplateFileResponse {
private String path;
private List<FileView> children;
}
I want to create an instance and set children is empty array. so what is the best way to do it?
You can create an empty list with the new operator:
public class TemplateFileResponse {
private String path;
private List<FileView> children = new ArrayList<>();
}
You may also want to initialize the path field, either in a constructor or inline, because otherwise it will be initialized to null by default.
I suggest that you read a tutorial about Java classes, constructors, methods, and instantiating objects to understand how all of this works.

How can I initialize an element of an array field? [duplicate]

This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
How to initialize an array in Java?
(11 answers)
Closed 3 years ago.
I am having trouble initializing an array element inside my class. How can I assign an initial value at a specific index of my array field?
public class Account{
int num[] = new int[50];
// I can't assign a value like this:
num[0] = 12345;
}
You can initialize the array in the constructor.
Just write:
public Account(){
num[0]=12345;
}
inside your class.
The other possibility is to use an initialisation block(but this is less flexible and difficult du document):
{
num[0]=12345;
}
(Also in your class)

Why can I access a private instance variable without a getter method? [duplicate]

This question already has answers here:
Why can I access my private variables of the "other" object directly, in my equals(Object o) method
(2 answers)
Closed 3 years ago.
I have two objects from the same class, calling it 'first' and 'second'. I have a method that takes in an object, so I use the 'first' object, call that method, pass in the 'second' object into that method.
Inside that method, why can I access the private instance variable of the 'second' object? Am I making any sense?
// Day.java, basic example of my question
public class Day{
private int stuff = 1;
public Day(int stuff){
this.stuff = stuff;
}
public int m(Day d){ // This method takes in an object as a parameter
int add = 0;
add = this.day + d.day; // why can you do this? isn't "day" private?
return add;
}
}
Source https://www.geeksforgeeks.org/access-modifiers-java/
You are accessing it from the same class
Check this Table Out..
Because you're still accessing your variable from within the same class it belongs to.
private variables are not visible from outside the class: but they are fully visible from within the class to which they belong.
If you tried to instantiate your class inside a different class, you'd have to use a getter to gain access.

How do I give a predetermined name to an object? [duplicate]

This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 8 years ago.
I am trying to create a method that contains a for loop that creates new objects, but I am stuck at how to assign a predetermined name to an object based on the loop's count. For instance it would be something like this:
private void createPictureObject(int count){
for(int a = 1; a <= count; a++){
Picture picture*a* = new Picture(arguments);
}
The result of this loop would be that I have objects named something like picture1, picture2, picture3, picture 4 etc. Is this even possible?
No You cannot do that! Dynamic variable naming is not allowed in Java. Use an array instead.
Picture[] pictures = new Picture[10];

Categories