MY mENU


Wednesday 9 January 2013

Access Modifiers in Java

The keywords which define accessibility permissions are called accessibility modifiers. Java supports four accessibility modifiers to define accessibility permissions at different levels.  In java, we have below 4 accessibility levels
1. Only within the class
2. Only within the package
3. Outside the package but only in subclass
4. From all places

Accessibility modifier keywords: To define the above four levels we have 3 key words.

  1.  Private: the class members which have private keyword in its creation statement are called private members. Those members are only accessible with in that class. 
  2. Protected: the class members which have protected keyword in its creation statements are called protected members. Those members can be accessible with in package from all classes, but from outside package only in subclass that too only by using subclass name or its object. 
  3. Public: the class and its member which have public keyword in its creation statement are called public members. Those members can be accessible from all places of java application. 
In class or its member’s declaration if we do not use any of the above 3 accessibility modifiers, the default accessibility level is package.
  • Default and public are the accessibility modifiers allowed for a class. 
  •  4 accessibility modifiers are allowed for class members. 
  • The default accessibility modifier of interface is package level and its member’s default accessibility is public.
Example:
class AccessModifierEx 
{
private static int a=10;
static int b=20;
protected static int c=30;
public static int d=40;

public static void main(String[] args) 
{
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);
}
}

Access Modifiers in Java

The keywords which define accessibility permissions are called accessibility modifiers. Java supports four accessibility modifiers to define accessibility permissions at different levels.  In java, we have below 4 accessibility levels
1. Only within the class
2. Only within the package
3. Outside the package but only in subclass
4. From all places

Accessibility modifier keywords: To define the above four levels we have 3 key words.
  1.  Private: the class members which have private keyword in its creation statement are called private members. Those members are only accessible with in that class. 
  2. Protected: the class members which have protected keyword in its creation statements are called protected members. Those members can be accessible with in package from all classes, but from outside package only in subclass that too only by using subclass name or its object. 
  3. Public: the class and its member which have public keyword in its creation statement are called public members. Those members can be accessible from all places of java application. 
In class or its member’s declaration if we do not use any of the above 3 accessibility modifiers, the default accessibility level is package.
  • Default and public are the accessibility modifiers allowed for a class. 
  •  4 accessibility modifiers are allowed for class members. 
  • The default accessibility modifier of interface is package level and its member’s default accessibility is public.

Why pointers are eliminated from Java?


  1. Pointers lead to confusion for a programmer.
  2. Pointers may crash a program easily, for example, when we add two pointers, the program crashes immediately. The same thing could also happen when we forgot to free the memory allotted to a variable and reallot it to some other variable.
  3. Pointers break security. Using pointers, harmful programs like virus and other hacking programs can be developed. Because of the above reasons, pointers have been eliminated from java.

Java Basics


Can we create empty java file, can we compile and execute it?
Yes, but after compilation we do not have .class. so we cannot execute.

Can we create empty class; can we compile and execute it?
Yes we can create and compile empty class. Compiler generates .class file with that class name. we cannot execute this class because this class does not have main method. It leads to exception.

When should be the java file name and class name be the same?
If class is declared as public, file name should be the same as the public class name, else its name can be user defined.

If a java file has multiple classes what is the java file name?
Public class name. If there no public class than java file name can be user defined.
In java file we can define only one public class and multiple non- public class names.

If we compile multiple classes’ java file, how many .class files are generated by compiler?
Compiler generates .class files as many class definitions as we have in that java file. Compiler generates .class file separately for every class with that class name.

User Defined method and Predefined method: Developer defined method is called user defined or custom method. Java software developers given method is called predefined method. It means already implemented methods are called predefined methods. Ex: println () method.

User Defined and Predefined classes: Developer defined class is called user defined or custom class. Java Software developers given class is called predefined class. It means already implemented classes are called predefined classes. Ex: System, String…

If a class does not have main method, how can we execute that class user defined methods?
We should use another class main method. We should call this method with its class name. In projects, we do not write main method in every class. Instead we write main method in one class, and we will call all other classes’ methods from this class’s main method for testing.

Basically main method is given to start class logic execution but not for developing logic directly.

We can define user defined methods with same name in multiple classes. While calling those methods we must use that method’s class name.
We can call main method explicitly with this syntax:  main(new String [0]);

Println () method places the cursor in the next line after printing current output. So that the next coming output will be printed in next line.
But whereas print () method places the cursor in the same line after printing current output. So that next coming output will be printed in same line.

Naming Conventions:

Naming a class: class name should be “Noun”, and should be in title case “every word first letter should be capital”

Naming a Variable: Variable name should be “Noun”, and should be in case “every word first letter should be small after that every word first letter should be capital”

In final variables all its letters should be capital and words must be connected with ‘_’. 
MIN_BALANCE

Naming a Method: Method name should be “Verb”, because it represents action, and should be in case “every word first letter should be small after that every word first letter should be capital and should follow ()
Ex: getUserName()

Naming Package: all letters are small, and its length should be a short as possible.