Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623

Notice: Undefined offset: 2 in /home/nhth4zhnn5bg/public_html/deegeu/wp-content/themes/Divi/includes/builder/feature/CriticalCSS.php on line 623
How to create Java blocks in your Java applications - J016 - DeegeU

How to create Java blocks in your Java applications – J016

by May 22, 2015




DeegeU Java Course

This video “How to create Java blocks in your Java applications” is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Try at home

  1. Try experimenting with variable scope.  Create variables that are in scope, and out of scope to see the difference.
  2. Try creating errors in your program and compiling it

Transcript – How to use Java Blocks

Hi there! We need a group Java statements together in to blocks. By blocks I mean a block of statements. There are rules though on how Java works between the blocks. We’re going to look at how we create Java blocks, how we put them together, and how we start to build these blocks into a full Java application.

In this lesson we’re going to answer what is a Java statement. We’ll look at how to group these statements into a Java block. With Java blocks we’ll need to understand variable scope as we nest the Java blocks. We’ll also see firsthand why we need to use braces to define blocks even when it’s not a hundred percent necessary.

Let’s start with what is a Java statement. You can think of a statement in Java similar to a simple sentence. I say simple because each Java statement should do just one thing. In Java you can also group statements and represent the collection of statements as one statement. In Java, statements are read in your program top-down and then left to right.

Declaritive and control Java statements - Free Java Course Online

Java has two types of statements, declarative and control.

Each Java statement represents a command to execute. With each statement you’re basically telling Java “go do this”. Java has two categories of statements available. Declarative statements and control statements. Declarative statements do exactly what it sounds like, declare a fact. I declare a variable of type int named “i” to be equal to 42. We use declarative statements to assign values to variables.

We also use declarative statements to change the value of variables. So later on we might say “i” is now 100. That is also a declarative statement. Control statements help you control the flow the Java application. Java has control statements for making decisions, performing loops, and branching to other sections of code.

A decision control statement would state, if something’s “true” run this block of code. A looping control statement says, run this code over and over and over until I say stop. Branching control statements tell Java to jump over here and run this block of code. An example of running a block of statements is when we print things to the screen. Lots of things happen behind the scenes, but we represent it with this one statement.

So what would we call a group of Java statements? We call a collection of Java statements a Java block. Now just like the toys, we connect these blocks with control statements to form a Java program. Blocks can then be assembled into larger blocks. Blocks are usually defined by starting brace and an ending brace. Let’s take a look at the blocks.

A Java program is really a big block made of many little blocks. The important takeaway here is your Java application is one big block composed of smaller blocks. Inside your Java program you’ll have smaller blocks of code. Often these are methods or classes.

We’ve seen this before. This is the main method which is the start of every Java application. The main method is a Java block. Its enclosed in the larger block up the Java application. The larger block is made from and encloses smaller blocks. We can keep creating blocks inside blocks inside blocks to infinity… It’s blocks all the way down. In this case an if-then control statement, something we’re covering in the next lesson, contains another Java block. At each level you can see the braces that enclose the Java block.

Empty Java block - Free Java Online Course

An empty Java block is valid.

Of course you don’t need braces for every block. otherwise every statement would need braces around it. Java already has a bad reputation for having too many braces, and that would just make it worse. You can have a block that does not have braces as this case shows. The if-then control statement can branch to block that is a single statement. This is bad form though, and you really don’t want to do it. Now some might argue this point, but spacing is optional and is inconsistent between developers.

Look at what happens if someone thinks you’re adding a statement to the orange block.

Because of the spacing, they might miss that the orange block is missing braces. From the indenting, it looks like the two statements are part of the same block.

Here’s what this code really looks like.

The expected behavior is the second print statement is also not run, but since is really part of the blue block, and not the orange block, the second print statement will always be run. Let’s run this in code and see what happens. Here we have the code exactly how we had it in the slide.

We’re going step through this program using the NetBeans debugger. I’ll show you how to do that. First we’re going to set a breakpoint on line 12. What a breakpoint is, is it tells our debugger stop here when we start running our code. And then we can implement our code by one step at a time. So we can actually see the flow of our application. Now that we’ve set our breakpoint, what we’re expecting here from the indentation is this program will run. It will print out byte b = 42, and then since the block is false, it shouldn’t print out inside block and not inside block.

So we go to “Debug | Debug project”.

And then it stops on our breakpoint. So next we’re going to hit this button, “Step over expression”. We go, and it’s the system print out “b = 42”, and prints it out. Then it should be done once it hits the “if (false)”, but it runs the not inside block statement. That isn’t the expected behavior from way it looks. So our code had “byte b = 42”, and “not inside block”. The moral of the story is don’t do this! It’s crossing the streams bad. You never know what’s going to happen. Now let’s take a look at scope.

In the blue block, we’re declaring a variable b and printing it to the screen. This works because both the declaration and the print are in the same block. Now look what happens when we declare a variable inside the orange block. Here we’re declaring a variable “k” and setting it equal to 24. The declaration happens inside the orange block.

So what do you think happens when we try to print “k” in the blue scope?

Well let’s go try. Okay we have the code exactly as we have it in slide. First thing we’re going to do is try to run it. And we hit “Run | Build project” and we get a compilation error. This code won’t even compile. The error is it cannot find the symbol on line 18. Now if we look closely in our code, we would have seen the little red mark underneath our “k”, which would have told us “Cannot find symbol, Symbol variable k”.

The reason is k is not in scope here. The only place k exists is inside the block of this if statement. So what we’ve seen is, k is not visible in the enclosing scope. You cannot see k in the blue block. It only exists in the orange block. So what do you think will happen if we try to print b in the orange block?

Let’s go try it! Okay we know that we can’t print k here, so let’s go ahead and delete that out. Let’s see if we can print b inside the if block. So b is declared above, outside of the if block. Okay, looks good so far. And we were able to print b. So in this case, b is visible.

Variables can be seen only in the block it’s defined and in any in block it encloses. So in this case b can be seen, or is “in scope”, in the blue and orange blocks. The k variable cannot be seen from the blue scope. It’s said to be “out of scope” from the view of the blue block.

Scope sometimes confused with the lifetime with the variable. Sometimes this is true, but we’re going to see in other lessons, the lifetime of the variable can extend past the scope where the program is currently running. Right now, just understand how scope works.

The last thing to note is empty blocks. You’ve seen your statement can be a complete block without braces. You can also have braces with no statements inside. This defines an empty block. If you ever find yourself needing an empty block, leave a comment inside the empty block noting you did that on purpose and why. Usually there’s no reason to have an empty block. But you can do it and there’s few instances where it might make sense like in interfaces. We’ll cover that much later.

Okay. In this lesson we’ve learned that a Java statement can either be a declarative statement or control statement. Declarative statements declare facts. And control statements control the flow of your application. We’ve covered a ton of declarative statements, so let’s start looking at control statements in the next lesson!

Hey! Thanks for watching the video. There is a quick quiz for this on DeegeU.com to if you’d like to gauge how much you learned. If you like the videos are seeing, please let me know by liking the video and hitting the subscribe button for the DeegeU channel on YouTube. I’d really appreciate that! If you have concerns or questions please leave them in the comments below or on DeegeU.com. There’s a poll on the front page of DeegeU.com so you can also let me know the topics covered next. Thanks for watching, and see you in the next video!







Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-java-intro.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This