Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Simple 10 second question for if an integer is defined in blocks of code

Hey, maybe a learning experience for new people. Watching something on youtube it said that a string/integer is defined in each new block of code. So why is 9 still showing instead of 6 in the console After watching this video on blocks of code I thought that after the variable is defined again then it reads reads down the page.
Java:
package testingintegers;

public class testingintegers {

    public static void main(String[] args)
    {
    int a = 9;
    
    System.out.print(a);}
{   int a = 6;
System.out.println(a);
}}


Thank you in advance again. Everyone's help on this forum makes me feel both inferior and insecure. Thank you smart people!!:)
 
Hey, maybe a learning experience for new people. Watching something on youtube it said that a string/integer is defined in each new block of code. So why is 9 still showing instead of 6 in the console After watching this video on blocks of code I thought that after the variable is defined again then it reads reads down the page.
Java:
package testingintegers;

public class testingintegers {

    public static void main(String[] args)
    {
    int a = 9;
  
    System.out.print(a);}
{   int a = 6;
System.out.println(a);
}}


Thank you in advance again. Everyone's help on this forum makes me feel both inferior and insecure. Thank you smart people!!:)

Code:
public class testingintegers
{
    public static void main(String[] args)
    {
        int a = 9; 
        System.out.print(a);
    }

//is this supposed to be a new method?
    {
        int a = 6;
        System.out.println(a);
    }

}

There, now that we have added some styling lol... a lot better to see. Now, the reason why you are getting an output of 9.. is because in java, as in c#, the
Code:
public static void main(string[] args)
, or "the main method", is the driver of the program. This is where you would put code in that you want to get executed. Any method/function outside of the main method, will not be executed, unless your call it in the main method.
 
As often, Antero360 beat me to it 🙂 I was going to say that you are a victim of your bad indentation. Meticulously lining up and indenting your code is the key to understanding and ease of maintenance. By all means, always do it as in Antero's reply, which is also my preferred style. Matching curly brackets should always line up. Don't believe anyone who says having the opening bracket at the end of a line is "better" !

Also note that Java is not like Javascript, where you can have anonymous bits of code all over the place and they will all be executed.
 
I know this is an old topic, but it’s an interesting one that hasn’t had the correct explanation for the original code yet, so here goes.
The block with a=6 is what’s called an instance initialiser . It’s simply enclosed in {} in the class but outside any method definitions.
when you create a new instance of the class any instance initialisers are executed before the class constructor is executed.

so what’s happening here is;
the instance initialiser will print 6 when it is executed
the main method will print 9
the code does not create an instance, so the instance initialiser is never executed
the main method is executed, so you see 9 as the output.

If you create a new instance in the main method you will see the 6 being printed.
 
I know this is an old topic, but it’s an interesting one that hasn’t had the correct explanation for the original code yet, so here goes.
The block with a=6 is what’s called an instance initialiser . It’s simply enclosed in {} in the class but outside any method definitions.
when you create a new instance of the class any instance initialisers are executed before the class constructor is executed.
Indeed so ! I had forgotten all about this somewhat arcane feature. Personally I don't see the point of it. Doing stuff when an instance is created is what constructors are for, are they not ?
 
Arcane indeed. You may see them:
instead of a constructor in an anonymous inner class (this is the only necessary use)
or
to initialise a map or whatever next to where it’s declared (i do this fairly often)

on the other hand static initialiser blocks can be very useful to initialise class variables when the class is loaded (eg if the initialisation code can throw an exception).
 
Yes I get it. However my money is still on using (an) appropriate constructor(s) to initialize stuff. I must admit to hating anonymous functions or code blocks at the best of times though 😁
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom