Article

Detail Description about Core Java Basics

Topic: Coach Training and CertificationPublished December 16, 2017

Legacy signals

Legacy popularity: 519 legacy views

Java Access ModifiersrnJava access modifiers are used to provide access control in java. Java provides access control through three keywords – private, protected and public. We are not required to use these access modifiers always, so we have another one namely “default access“, “package-private” or “no modifier“.rnWe can use java access modifiers with Classes as well as Class variables and methods.rnWe are allowed to use only “public” or “default” access modifiers with java classes.rn1. If a class is “public” then we can access it from anywhere, i.e from any other class located in any other packages etc.rn2. We can have only one “public” class in a source file and file name should be same as the public class name.rn3. If the class has “default access” then it can be accessed only from other classes in the same package.rnJava Access Modifiers with Class MemberrnWe can have all the four access modifiers for class member variables and methods. However member access modifier rules get applied after the class level access rules. For example, if class is having default access then it will not be visible in other packages and hence methods and variables of the class will also be not visible.rnJava Access Modifiers – public keywordrnIf class member is “public” then it can be accessed from anywhere. The member variable or method is accessed globally. This is simplest way to provide access to class members, however we should take care in using this keyword with class variables otherwise anybody can change the values. Usually class variables are kept as private and getter-setter methods are provided to work with them.rnJava Access Modifiers – private keywordrnIf class member is “private” then it will be accessible only inside the same class. This is the most restricted access and the class member will not be visible to the outer world. Usually we keep class variables as private and methods that are intended to be used only inside the class as private.rnJava Access Modifiers – protected keywordrnIf class member is “protected” then it will be accessible only to the classes in the same package and to the subclasses. This modifier is less restricted from private but more restricted from public access. Usually we use this keyword to make sure the class variables are accessible only to the subclasses.rnJava Access Modifiers – default accessrnIf class member doesn’t have any access modifier specified, then it’s treated with default access. The access rules are similar as classes and the class member with default access will be accessible to the classes in the same package only. This access is more restricted than public and protected but less restricted than private.rnThats all for the java access modifiers, it’s simple to understand. Just don’t confuse with default and protected access.rnEasy way to remember is that default access is more restricted than protected and protected members are accessible in subclasses.rnJava for looprnJava for loop is used to iterate over a range of values. We can use for loop to iterate over an array.rnJava for looprnThere are three types of for loop in java.rn1. General for looprn2. for-each or enhanced for looprn3. Java For loop with labelrn4. “variable initialization” happens only once when for loop begins execution.rn5. “termination condition” should result in boolean expression, if it returns false then for loop terminates.rn6. “increment/decrement” operation is performed after each for loop execution. In most of the scenarios, it should lead towards the termination condition unless you want for loop to not terminate at all.rnJava for each looprnJava for each loop is also called enhanced for loop. We can use for each loop to iterate over array or collection elements. Java for each loop is the recommended way wherever it's possible to use it. It's very easy and compact to write.rnJava for loop with labelrnWe can add a label to for loop, it's useful with break and continue statements to get out of outer loop. Note that by default break and continue statements work with inner loop only.rnJava while looprnJava while loop is used to execute a block of statements continuously till the given condition is true.rnNotice that I am increasing value of i inside while loop, otherwise the while loop will never terminate and we will have an infinite loop. The only way to terminate the program running in infinite loop is to manually quit it or when the JVM runs out of memory.rnAlso note that if boolean expression returns false then statements inside while loop won't execute. So there is a chance that statement inside while loop will never execute.rnwhile true javarnSometimes we intentionally want our while loop to run infinitely. In that case we can use while true loop. An example could be looking for a file at specific location continuously and if found then process it.

Further reading

Further Reading

4 total

Article

How Coaching Turns Knowledge Into Change Walk into any bookstore or scroll through your favorite podcast feed, and you’ll see the same thing: endless tips, strategies, and “life hacks” promising transformation.rnWe devour them. We highlight paragraphs. We even try to practice what we’ve learned. But somehow, the big shifts never stick. Weeks later, the old patterns creep back in.rnSo why does it happen? Why do so many smart, motivated people keep getting stuck — eve

October 6, 2025

Article

grinding and cutting a variety of materials across numerous industries. Understanding their construction, types and safety precautions is mandatory for anyone working with these powerful tools. This article will explore the basics of abrasive wheels, offering insights into their components, maintenance and legal requirements to ensure both effective and safe usage. Exploring the Basics of Abrasive Wheels Abrasive wheels are critical tools in various industrial applications, f

March 6, 2025

Article

The internet we use daily, known as the surface web, represents only a fraction of the entire digital landscape. Beneath this visible layer lies the deep web and the dark web, where anonymity, privacy, and unrestricted information exchange thrive. Among the most well-known directories for accessing dark web content is the Uncensored Hidden Wiki . This article delves into the history, significance, risks, and access methods of the Uncensored Hidden Wiki, providing a comprehens

January 31, 2025

Article

If you're searching for exceptional piano classes near you, there are several excellent options in Toronto to consider. For those eager to learn the piano, finding a school or instructor that offers expert guidance, comprehensive lesson plans, and a supportive environment is key. Catering to both beginners and advanced players, many schools in the Toronto area provide tailored lessons to help students achieve their musical aspirations. What to Look for in Piano Lessons When

January 1, 2025