This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I found an instantiation wich I do not understand how works.
The instantiation looks like this:
public static Class instance[] = new Class[arraySize];
If my guessing is right, the instance is an array?
How will this work?
This declares an array of Class objects references. It is equivalent to the other syntax of [] after the type.
You would access it just like a normal array:
instance[0] = ...
instance[1] = ...
public static Class instance[] = new Class[arraySize];
public is the access modifier. This one means that this variable is visible in your entire project
static means that this variable is a "class" field it means that it belongs to the entire class and you can access it by ClassName.nameOfTheVariable or if you are accessing it from inside of class it is declared you could use just nameOfTheVariable.
Class in this context is a type and you should treat is as a type of object
[] means that this is an array you could also write Class[]
= is assignment operator
new is the word that annunciates that there will be memory allocation and constructor invokation after it
After new there is initialization of array of arraySize length.
Related
This question already has answers here:
What is the difference between field, variable, attribute, and property in Java POJOs?
(11 answers)
Closed 1 year ago.
Is the correct terminology in Java attributes or class variables or class fields etc? Many thanks
This to confirm is for variables declared in the class Main...I've seen these variables called a few things and just curious what the correct term for Java would be?
e.g.
public class Main {
int x = 10;
int modelYValue;
String modelName;
Boolean joyous;
private int age = 28;
}
Those are instance members, or instance variables. When you create an object those members get created as part of it. Each object has its own instance member variables. See https://en.m.wikipedia.org/wiki/Instance_variable
Class member means the variables belong to the class, not to any object instance. Static variables are class members.
People tend to confuse these and say class member when they mean instance member.
This question already has answers here:
How do I create a static local variable in Java?
(8 answers)
Closed 6 years ago.
I know that in C, when the keyword static is used on a local variable, it causes that variable to remain initialized between function calls (i.e. when the variable goes out of scope). For example:
int myFunction() {
static int i = 3;
i++;
return i;
}
If myFunction() is called twice, it will return 4 the first time and 5 the second time (because i keeps its value between the two calls rather than being reinitialized the second time).
My question is this: does Java have an equivalent keyword to static in C? Java also has the keyword static, but it is used completely differently than in C.
Not exactly, but a private static class level variable will almost do the same thing.
But
it will be visible to all other methods of the class as well
it will be initialized not on first method call, but when the class itself is loaded
I suppose that is workable.
All variables in a method are local to the function and placed on the stack. The closest you have is a static variable in a class.
If you make the variable private and place the method in a class of it's own you will achieve much the same result.
(With a private constructor)
It's somewhat similar to the private keyword, as in C a static global variable or a function is visible only in the file it's declared in...
So that's probably the closest you will find :)
No, because all methods and functions are bound to classes in Java, so there is no "global space" as there is in C. The static keyword in Java has different semantics.
See the docs for static and this post for more detail on the differences.
Java uses static in a different manner. To get the same result you want here, you should use a private field instead.
This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 7 years ago.
I am just curious ! When I use an int table, I can get to .length, which returns the length of the current table. For exemple :
int b[] = {0,1,2};
int l = b.length; // which is equal to 3
What I want is to get to ".length" in the Java documentation. In order, to figure out if ".length" is a static method or instance variable , and things like that.
From the JLS
10.7. Array Members
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.
All the members inherited from class Object; the only method of Object that is not inherited is its clone method.
This question already has answers here:
What does .class mean in Java?
(8 answers)
Closed 8 years ago.
What exactly is the meaning of the following construction: MyClassName.class ?
At first I thought MyClassName.class would represent the access of a static class variable defined for MyClassName class, but it that'd be true then the following assignment should be possible:
MyClassName m = new MyClassName();
Class<MyClassName> clazz = m.class; //access static class variable by using an instance variable
What is the true meaning of MyClassName.class? Is it a static class variable? is it a Java special construction?
Every class in Java has an associated instance of java.lang.Class which contains metadata about the class i.e its attributes, types, methods, super class etc.
Here MyClassName.class is called class literal. Link to Java doc for further info - http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.8.2
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java: how to call non static method from main method?
I am finding this a bit hard to implement.
I have a String[] called name which is declared globally.
String[]name;
Now i want to access this variable from the main() method. The main method is static therefore how could i access it.
I tried Animal.name but it didn't work.
How can i do this?
You need to create an instance of Animal class to access instance fields: -
Animal animal = new Animal();
animal.name; // Access array
You can solve this with two different ways, each requiring code modification:
First is to create an object of Animal type and accessing the name property.
Second is to make name as static.
like this: static String[] name = new String[10];