123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Education >> View Article

Detail Description About Core Java Basics

By Author: Infocampus
Total Articles: 281

We can use java access modifiers with Classes as well as Class variables and methods.
We are allowed to use only “public” or “default” access modifiers with java classes.
1. If a class is “public” then we can access it from anywhere, i.e from any other class located in any other packages etc.
2. We can have only one “public” class in a source file and file name should be same as the public class name.
3. If the class has “default access” then it can be accessed only from other classes in the same package.
Java Access Modifiers with Class Member
We 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.
Java Access Modifiers – public keyword
If 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.
Java Access Modifiers – private keyword
If 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.
Java Access Modifiers – protected keyword
If 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.
Java Access Modifiers – default access
If 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.
Thats all for the java access modifiers, it’s simple to understand. Just don’t confuse with default and protected access.
Easy way to remember is that default access is more restricted than protected and protected members are accessible in subclasses.
Java for loop
Java for loop is used to iterate over a range of values. We can use for loop to iterate over an array.
Java for loop
There are three types of for loop in java.
1. General for loop
2. for-each or enhanced for loop
3. Java For loop with label
4. “variable initialization” happens only once when for loop begins execution.
5. “termination condition” should result in boolean expression, if it returns false then for loop terminates.
6. “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.
Java for each loop
Java 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.
Java for loop with label
We 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.
Java while loop
Java while loop is used to execute a block of statements continuously till the given condition is true.
Notice 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.
Also 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.
while true java
Sometimes 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.

Total Views: 191Word Count: 738See All articles From Author

Education Articles

1. Exam Nse7_efw: Enterprise Firewall – Fortios 5.4 Exam
Author: Jessica Doyle

2. Prepare Your Kid For Playschool For His Own Betterment
Author: Bubbles Playway

3. Top 21 Prototyping Tools For Ui And Ux Designers 2018
Author: gyanguide

4. Use Advanced Digital Marketing To Become A Top Professional
Author: Mohini Singh

5. Details On Nursing Course In Mumbai!
Author: Rakesh Salunke

6. Take First Step Towards Success Through Learning French Language
Author: Ben Kachimo

7. Sql Server Dba Training Institute In Ameerpet Rr Technosoft
Author: gomathi

8. Need To Know Gmat Exam Rules, Regulations And Guidelines
Author: Brighter Prep

9. Geometry Homework Help Can Be Relieving To The Students
Author: Weston Young

10. Accept The Challenge Of Passing The Nse4-5.4 Exam On The First Attempt
Author: Jessica Doyle

11. Exam Hpe6-a44: Aruba Certified Mobility Professional (acmp) V8 Certification.
Author: Jessica Doyle

12. Reasons To Switch To Geographic Information System (gis) For Civil Engineering
Author: NIBT

13. What All You Will Learn From Iot Training Program?
Author: Multisoftsystems

14. Digital Marketing Course For Graduates
Author: Yogesh Sash

15. Danger In The Dust: Asbestos Awareness And Controlling
Author: Chris Haris

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: