Java Fundamentals

Java Fundamentals instanceof Pattern Matching

Learning Paths:
Check out a free preview of the full Java Fundamentals course:
The "instanceof Pattern Matching" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses using the instanceof operator to perform two actions simultaneously with pattern matching. The scope of the newly created object is restricted to within the if statement's curly brackets.

Get Unlimited Access Now

Transcript from the "instanceof Pattern Matching" Lesson

[00:00:00]
>> Pattern matching, right? So in this condition, we not only check if Sasha is a dog. But if so, we cast Sasha as a dog, and we store it into this newly introduced sashaTheDog variable. So this is a shorthand way of writing it all out. So remember I said we could do it like if this is an instance of that, then do something, right?

[00:00:36] And maybe you want to do a typecasting or something like that. With this pattern matching, it allows you to essentially perform two actions in one. So that top if clause is the exact same thing as the bottom one. That's just a shorthand way of doing it. That is called the instance of pattern matching.

[00:01:04] So basically, you're saying, if Sasha is an instance of dog, then create a new dog object called sashaTheDog, and then cast Sasha to the dog. So you could say all of that by just saying sasha instanceof Dog, sashaTheDog, same thing.
>> And they call that instance of pattern matching?

[00:01:29]
>> Instance of pattern matching.
>> Quick question about it.
>> Yeah.
>> So for this instance of pattern matching, the scope of sashaTheDog is, I'm guessing, the scope of the curly brackets?
>> Lovely, look at you. Yes, so you've defined, basically defined a new variable inside of that if clause which means that sashaTheDog will only be accessible within that scope.

[00:02:00] I love that question so much.
>> You could change it to that way. You could set that as a global. You wouldn't necessarily have to do it inside, or you could set it outside.
>> If you wanted to do it, I don't think you could do that as a global, not with the instance of check.

[00:02:21] Because at the global level, outside of methods, the only thing you can do really is kind of declare fields. So this logical check here where you have an if statement would not be allowed outside of a method You see what I'm saying? Let me show you. So I couldn't put an if statement right here.

[00:02:54] Well, we don't even know rocky or anybody cuz it's outside of scope. But it's already complaining even before I got to rocky, cuz I'm outside of a method, any executable code like this needs to be inside of a method.
>> So the only way to do it is maybe if you do it like it's a Boolean or if it returns true, then it saves the value to an experiment.

[00:03:15]
>> Yeah, you could do something like you could declare, I don't know, like Dog sashaTheDog up top, and then do some, I don't know, something like that. I don't know, that's kind of wonky.