Java for Beginners 5 – access and non access modifiers

Java for Beginners 5 – access and non access modifiers

Hi welcome to my free Java Video Course lesson 5. My name is Marcus Biel I’m a software craftsman with 14 years of
experience in Java my goal is to teach you Java in the
easiest possible way please send your feedback to [email protected] Let’s recap what we’ve learned so far. We’ve created a class Person here it
starts with the opening curly brace and it ends with the
closing curly brace we had two constructors defined one default constructor that does not
have any parameter and one constructor that has a name parameter; This is the equivalent of the instance variable here and so that Java can differentiate between the parameter and the instance variable we added “this”. And now when someone calls the constructor the given
person name will be assigned just in time when the
code is executed to our instance variable inside of the
class which is private so it’s not visible for a caller We added a comment because for empty constructors it’s best practice to add a comment so that your colleagues know this was done on purpose because often I mean when you have for
example a method and its empty this could be a bug, so this way they will know you did this on purpose. I also
shortly mentioned there are other types of comments but this is kind of my strategy in this
course to not give you too many options for the
beginning I mean learning something new is already quite difficult so I don’t want to confuse you too much
this is the best practice anyway so always use that kind of comment and
sparsely use comments, really comments should not be a replacement for bad code in 99 percent of the cases it’s
better to create a method instead of a comment but I’m going to much in details we’ll
see this later on. anyway so we defined two methods in “lesson 4” we created this hello() method that takes a parameter String of name “name” and this String we concatenated with this “Hello” String so that the caller in our case our PersonTest got returned a “Hello” and in our case “Hello Marcus” returned. And this we tested. So now I would like to talk about modifiers first of all about visibility modifiers, actually we already
talked about them a bit one visibility modifier is private another one is public so public I think I already said this class is visible or usable from all other classes in all other packages. This class now Resides in the package com.marcusbiel.javacourse.lesson2 so here for example “Name” is in “lesson3” so if you would go in the code of the “Name”class it could easily use “Person” as long as it’s public. A private
class – we can try that. No. It says “Modifier private not allowed here” and of course, how would that make sense? I mean a class that is not usable anywhere but just in this class – this doesn’t make
sense. But actually there’s one more option we can
leave the modifier away and this will give us
the default modifier this is also why it’s called “default modifier” or also “package level” modifier because now person can only be used from other classes within this package and we can do the same for variables, the
constructors we can even have a private constructor we could have that public, you could have that method private, and this one default and we can even call let’s use this one. Let’s call helloWorld() from this method here. All this is
working and this actually even makes sense
because especially when your methods are growing I
personally would say not more than three or ten lines – then to break things up you can split a long method into smaller private methods and then calling the private method. Like I mean here we have only two methods so to make things up you can call one method three times but of course I
could define further methods we don’t have this now and then you can call any number of methods later on. So to recap visibility modifiers we learned so far are “public” “private” and “default” or “package level” besides there are other visibility modifiers one for example is “protected” but we don’t need this at the moment so you should be perfectly coming along with just “public”, “private” and “default” so for now let’s just concentrate on these few let me fix the class shortly, because very soon we’re going to need it in our
test and I don’t want to adjust the test I
want to have the code as it was before because now I would want to show you a new
concept I have this idea I have prepared a test shouldReturnNumberOfPersons() – let me create a few objects of type “Person” – person1 new Person(). Person person2=new Person() we can also use the other constructor
but then I would have to create an object of “Name” as this at the moment is
just to much work for me I’m not doing it and I can also give it a different name
like “myPerson” just to show you, you can have any name for the variables. So what this means is when the test will be created here after this line is executed which is
just after the colon we will have one object in
memory two objects in memory and after this line
three objects in memory so what I want now – I want to test on a number of objects now I should have three objects
created and I want to create a method that tells me exactly that – just
dynamically myPerson.numberOfPersons() Okay still red. Remember? We’ll create it and TADA – “int” appeared because I mean three
is a number and a number in Java is an integer or here in short – an “int”. An int is actually a primitive and the default of this is zero so this is why my IDE puts “0” here so it’s not red anymore we can have a first run – but this will not work of
course because it statically returns “0” we
expect “3” so this is not what we want. Well of
course we could say “3” statically so now this should work. It’s a bit cheating of course – green bar – tadaaa! But I mean I want this to dynamically work. So when I only have 2 Persons – I want this to be 2 and well of course I have to rename my variable – let’s copy it – and now when I execute it of course this is not going to work expected “2” but it’s still “3”. So my first approach – the static one – always returning 3 – nice gag – but not what we need. So what we need is at a specific
time when each object is created we need to count each object that is created so what would be the place where the
object is created there is one thing similar to a method that is called and this is you might guess it the constructor – we used default constructor for creation so we would have to do something in here. Now I want to invent a variable – an
instance variable I also call it or let’s call it personCounter personCounter=and now I say personCounter because I want the value it had before and then I want to add “1” – to the last value of personCounter to have the new value dynamic so on the left side is the new value on the right side is still the value from the prior calculation and I add “1” to it. now this is a lot of code again – and this incrementation by “1” is used so often that they invented a short form for that which is “++” so with “++” we say take the current value of personCounter, increment it by “1” and save it again into the value of personCounter it’s still red because we still have to create it – let’s do that! we make it private – of course – it’s an int – I called it personCounter and “;” because I don’t have to say “0” I could also say “=0” but the default for int is “0” anyway. so what we usually do is – we just put it here – as is. So the idea would be it starts with “0” the first time this Constructor is called, it’s incremented by “1” and so on. So that my method now should not statically return “3” but personCounter instead. Let’s try that! Execute the method still not working! It returned “1”, it didn’t return “0” it didn’t return “3” it returned “1”. How’s that? we didn’t say “1” anywhere. Maybe you guess it already? so we started with “0” we incremented
by “1” now I told you something in the prior
session which is – this constructor is part of the instance so each object has it’s own
constructor has their own methods. So this is called for each of the objects – so in the end – both objects would have the counter set to “1”. So what we need is more like a global counter that
is not on the instance level but we say on the
class level because for all the objects of type
Person there’s only one class Person and this we achieve with the modifier called “static” However as a recommendation – because this will help you in the future – I won’t go in details why just believe me – as we have the variable of type static you should also have the method of type static. it would work also without that it’s just better – believe me. so now let’s execute it – and this works! Great! Let’s try another thing – let’s add this time four. Three and four and execute it. and it works! I said actually person2 why not saying
person1 and it works! well as you see this might be confusing
I mean which variable to decide on? and also there’s a recommendation actually what Java does is – Java sees that number of persons is a static
method it sees that you have an instance
variable of type Person and as this is a static method it doesn’t call the method on the object
because As this method is static, it’s not part
of the object anymore this is like moving it out of the object
this method now is part of the class so, Java does some magic here – is trying to be smart and is smart and
calls the right method of the class – so what we doing here is working because
Java knows how to do it properly but is not so nice. What is better – is direct saying “Person”. So that everyone knows you’re not going to any
specific instance you’re just calling a static method on the Person class. let’s execute that again – and it’s working! Thank you! Thanks for watching! Please send feedback to [email protected] [email protected] Bye!

4 thoughts on “Java for Beginners 5 – access and non access modifiers”

  1. Hi welcome to my free Java Video Course lesson 5. My name is Marcus Biel I’m a software craftsman with 14 years of experience in Java my goal is to teach you Java in the easiest possible way.
    RecapLet’s recap what we’ve learned so far. We’ve created a class Person here it starts with the opening curly brace and it ends with the closing curly brace. We had two constructors defined. One default constructor that does not have any parameter and one constructor that has a name parameter….

  2. This is cool so far…………you putted so much effort on all the courses, this quality deserves so much money
    Sadly, most of the paid courses aren't that good but there are some other that is good like your course

  3. h sir marcus , i have followed your instructions about putting static keyword in private variable integer personCounter and public method integer numberOfPerson to count the Person object in PersonTest,java dynamically .. i have 2 two person objects under the shouldReturn method in PersonTest, java and my actual value supposed to count 2 objects but it is counting the rest the objects under the other test methods in PersonTest,java . Instead of having 2 in my actual value when i run the test it becomes 5. I want to know if there is any problem in my code.

Leave a Reply

Your email address will not be published. Required fields are marked *