What is the difference between a local variable, an instance field, an input parameter, and a class field with respect to a simple Java program?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
A local variable is a variable in a method. It's scope is limited to the scope of the two parenthesis around it. {}
Example:
With an instance field, I think you mean a member of a a class instance. If you take for example the class
Dimension
, this would beheight
orwidth
. . An input parameter is a parameter in a method, as you guessed.A class field is a field in a static method.
Not quite.
A class field is what you think a local variable is but it is generally a static field and so is the same across all instances.
An instance field is the same as a class field, but is non static and can be different for each instance of the object.
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
And a local variable is a variable inside a method or block, that can only be used by that method or block.
Oh and your input parameter definition is correct, an input parameter is a field that is passed to a method as a parameter.
A local variable is defined within the scope of a block. It cannot be used outside of that block.
Example:
I cannot use
local
outside of thatif
block.An instance field, or field, is a variable that's bound to the object itself. I can use it in the object without the need to use accessors, and any method contained within the object may use it.
If I wanted to use it outside of the object, and it was not
public
, I would have to use getters and/or setters.Example:
An input parameter, or parameter or even argument, is something that we pass into a method or constructor. It has scope with respect to the method or constructor that we pass it into.
Example:
Both
x
parameters are bound to different scopes.A class field, or static field, is similar to a field, but the difference is that you do not need to have an instance of the containing object to use it.
Example:
I don't need an instance of
Integer
to retrieve the globally known maximum value of all ints.Start by having a read through Classes and Objects
No, generally a local variable refers to a variable that only has context within the area it was declared. This typically refers to variables declared within methods and
{...}
blocks (likeif
statements)Not really, an instance field is any field, declared at the class level which is not static, therefore it's value only has meaning to an individual instance of the class
Yes
A class field and instance field are (generally) the same thing. The only difference would be if the field is declared
static
, then it can't be a instance field...A class field is often called a class variable, and you can find that information here
A local variable is local to a method.
An instance fields is the field of an instance of a class i.e. an object.
A parameter is passed to a method
A class field, I assume is a static field which is associated with the class. e.g. if you use multiple class loaders, you can have multiple classes with the same name and their own static fields.