What is the difference between a local variable, a

2019-01-03 10:39发布

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?

6条回答
贼婆χ
2楼-- · 2019-01-03 11:19

A local variable is a variable in a method. It's scope is limited to the scope of the two parenthesis around it. {}

Example:

public void someMethod () {

    int localVariable1 = 5;

    if (...) { 
        int localVariable2 = 7;
    }
}

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 be height or width. . An input parameter is a parameter in a method, as you guessed.

A class field is a field in a static method.

查看更多
狗以群分
3楼-- · 2019-01-03 11:22

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-03 11:27

A local variable is defined within the scope of a block. It cannot be used outside of that block.

Example:

if(x > 10) {
    String local = "Local value";
}

I cannot use local outside of that if 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:

public class Point {
    private int xValue; // xValue is a field

    public void showX() {
        System.out.println("X is: " + xValue);
    }
}

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:

public class Point {
    private int xValue;
    public Point(int x) {
        xValue = x;
   }

    public void setX(int x) {
        xValue = x;
    }
}

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:

System.out.println(Integer.MAX_VALUE);

I don't need an instance of Integer to retrieve the globally known maximum value of all ints.

查看更多
叼着烟拽天下
5楼-- · 2019-01-03 11:36

Start by having a read through Classes and Objects

I know the local variable is a variable that is available to the class it is in, correct?

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 (like if statements)

An instance field is an Object that is created in the constructor...?

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

An input parameter is what is passed into a method.

Yes

But I have NO idea about a class field!

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...

查看更多
放我归山
6楼-- · 2019-01-03 11:44

A class field is often called a class variable, and you can find that information here

查看更多
The star\"
7楼-- · 2019-01-03 11:44

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.

查看更多
登录 后发表回答