• 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.
    • 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!!:)
 

Antero360

King Coder
Staff Team
Security Analyst
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.
 

cbreemer

King Coder
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.
 
Top