Why Can't I Do Assignment Outside a Method

Why can't I do assignment outside a method?

you need to do

class one{
Integer b;
{
b=Integer.valueOf(2);
}
}

as statements have to appear in a block of code.

In this case, the block is an initailiser block which is added to every constructor (or the default constructor in this case) It is run after any call to super() and before the main block of code in any constructor.

BTW: You can have a static initialiser block with static { } which is called when the class is initialised.

e.g.

class one{
static final Integer b;

static {
b=Integer.valueOf(2);
}
}

Why doesn't this assignment work outside of a function?

When we write :

data_type variableName = someValue;

It means we are first declaring variableName to be a variable, and of the type data_type. Subsequently, an assignment of a value is being done, immediately afterwards and since this is the first value assigned to variableName, it's also initialising it to someValue.

Which is allowed. This is a special type of function, a system function, known as initialisation.

But writing

variableName = someValue;

means we are attempting to assign someValue to variableName outside the scope of any function, and outside the scope of an initialisation.

This is not possible outside a function.

Code only executes from within called functions, with the exception of initialisation, which occurs during an initial assignment.

Setting a variable outside of an instance method does not work

why am i not able to do this?

The short answer is: because it is not valid Java.

At the top level of a Java class, the only things that can appear are declarations and initializer blocks.

  • Declarations comprise field declarations, constructor declarations, method declarations and (nested) type declarations of various kinds.
  • Initializer blocks comprise static initializer blocks and instance initializer blocks. (Initializer blocks are rarely seen, so you may not have encountered them yet.)

In your "good" example, each one of the lines within the class is a field declaration. This one:

int speedometer = 78;

is a field declaration with an initializer.

In your "bad" example:

int speedometer;
speedometer = 78;

are two separate constructs. The first is a field declaration (without an initializer). That is allowed. The second is an assignment statement. An assignment statement is NOT a declaration, and therefore is not allowed at this point in a Java class.

Statements (in general) are allowed within the body of a method, constructor or a lambda, or within an initializer block.

Java: Why can't I call this method outside of main?

In Java, you can't have executable statements that aren't part of a method.* The first line is okay:

VelocityCounter caller = new VelocityCounter();

because the compiler thinks you are declaring and initializing an instance variable named caller for class Velocity2. The second line, however:

caller.VelocityC(6, 3);

is illegal at the top level of a class declaration.

*Technically, that's not quite right. Statements can also appear in constructors, static blocks, and instance initializer blocks.

Why can't I assign values to global variables outside a function in C?

This is a definition of a global variable, with the optional initialisation to a specific value:

int i = 8;

Note that it is not code which gets ever executed, the variable will just be set up to initially contain the 8. Either consider it "magic" (a helpful model for many things not really defined by the standard) or think of tables with values being copied to memory locations before any code is executed.

This is a piece of code which has no "frame" in which it is executed.

(Or you intend it to be. The compiler is of other opinion, see below.)

i = 9;

There is no function containing it. It is not clear when it should be executed. That is what the compiler does not like.

In C, all code has to be inside a function and will only be executed if that function is called, e.g. from main().

Other language, mostly those which execute "scripts" by interpreting them (instead of code being turned into executeables, e.g. by a compiler) allow to have code anywhere. C is different.

The compiler sees this differently:

i = 9;
  • it is not inside a function, so it cannot be code
  • it looks like a variable definition, assuming that you mean it to be an int, i.e. the default
  • but relying on defaults is not a good idea, so warn about missing type and that the default is used
  • also, if it is a definition, then it is the second one for i, now that is really wrong, so show an error and fail the compiling
  • just to be helpful, mention where the first definition of i is

That is how to read the compiler output you have quoted.

Why cant i assign at next line?

The non-static block (here marked as { } ) has access to the variables, and methods. And this block is called during the constructor execution, just after the call to the super class constructor.

Hence, initializations can be made in this block.

You can either initialize instance variables along with the declaration, in one line, or inside of a non-static block.

Then, this initialization takes place as soon as the constructor is called.
(Because, it follows the execution of the code in the block).

Hence, the block is effectively a part of the constructor execution.

On the other hand if you write your initialization code in the class itself, it has no meaning, as no method ever executes that line, hence is incorrect.

So, Method 1 : Initialization Along with Declaration :

class Foo {
Object x;
String s = (String) x;
}

Or, Method 2 : Initialization in block, after declaration :

class Foo {
Object x;
String s;
{
s = (String) x;
}
}

Remember, this non-static block is called during constructor execution, after the call to the super class constructor.



Related Topics



Leave a reply



Submit