Core Java

Static members…

1

Statics are a rather strange beast, they belong to no instance of a class, they have no fixed abode, other than their class..

Consider the following example :

Share

Local Variables, as far as the SCJP is concerned

0

Local variables are variables that are declared locally, funny that eh? This one should be nice and easy, lets have a look at a quick example :

Share

Other modifiers, for members…

0

We’ve already touched upon various modifiers, for classes (both access and non-access), but there are also some more modifiers for members, as detailed here.

We have the following modifiers for members :

  • final - Can’t be overridden
  • abstract - No implementation specified, subclass must implement
  • synchronized - Only a single thread of execution can pass through at a given time
  • native - Implemented by a 3rd party piece of code (C++ for example)
  • strictfp - Enforces foating point precision and doesn’t let the JVM do its own way
Share

Class Modifiers (non-access)

1

In addition to class access modifiers, classes can also be marked with non-access modifiers. These modifiers imply rules on a class, but are not necessarily linked to access rights.

The following non-access modifiers are available:

  • final - The class can’t be extended.
  • abstract - The class has to be extended, and can’t be instantiated on its own.
  • strictfp - Ensures that you always get exactly the same results from your floating point calculations on every platform your application runs on. If you don’t use strictfp, the JVM implementation is free to use extra precision where available.
Share

Class Access Modifiers

2

Class access modifiers define who can see the class, you use it on a daily basis, have a look at the following :

public class ClassAccessModifierExample
{
    public static void main(String[] args)
    {
        // do something!
    }
}

There you go, you said “public class”, thats you saying that this class is public, and anything can access it.

It is important to note, that even though a class may be declared as public, you can still have private members within that class. The class access modifier merely states who can gain access to the class.

Share

Varargs, for those times when you’re not sure how many parameters you’ll have…

0

As of Java 5, methods are now able to accept from 0 to many arguments. Sounds confusing, but you could actually be using it already without knowing, how about looking at your main methods?

public static void main(String... args)

As we can see above, var args are declared as TYPE… NAMEOFVARIABLE. Lets take a look at a basic example I’ve created :

Share

Coupling and Cohesion, what you need to know for SCJP…

0

Coupling and cohesion are two terms that often get mixed up, however they are actually really simple concepts, and the bonus is, they don’t just apply to Java.

Coupling

Coupling, in its purest terms, means “the degree to which one class knows about another class”. If one class uses another class, that is coupling. Coupling is everywhere, but the level of coupling varies.

Consider the following example of a tightly coupled set of classes:

Share

The Conditional (ternary) operator, cleaning up if-else assignments since 1996…

1

The conditional operator, or ternary operator as it is otherwise known is a great way for assigning variables based on boolean tests. For example you may have seen the following :

public class MonkeySniffer
{
    public static void main(String[] args)
    {

        int x = 5;
        String s;

        if (x < 10)
        {
            s = "Its less than 10";
        }
        else
        {
            s = "its greater than 10";
        }
        System.out.println(s);
    }
}

Pretty simple right? However that if-else statement is a bit messy considering all we’re doing is checking a condition and then using the output of that to assign a literal to our String reference.

Share

Looping in Java, a brief look at the various loops and how they can be applied

0

One of the great features of any programming language is the ability to repeat blocks of code, sometimes indefinately, sometimes until a certain condition is met, or for a set number of iterations.

Luckily, Java comes with several flavours of loops, let have a brief look at our options

  • The “for” loop – The for loop is generally used when you know in advance how many iterations the loop must execute. The for loop enables you to setup a repeatable code block with access to the index(es)
Share

Java; the for-each loop, perfect for fondling collections and arrays

0

The for loop is great, but is it really that nice when you want to iterate over an array or collection? You’d have to do something like the following :

import java.util.ArrayList;
import java.util.List;

public class MonkeySniffer
{
    public static void main(String[] args)
    {
        List<String> myList = new ArrayList<String>();
        myList.add("Hello");
        myList.add("James");
        myList.add("Elsey");

        for (int i = 0; i < myList.size(); i++)
        {
            System.out.println(myList.get(i));
        }
    }
}

Works fine doesn’t it? Looks a bit messy doesn’t it? No need for the index since we only ever use it to get the current value out of the list. Lets have a look at the for-each loop (often called the enhanced for loop) and how it can help

Share
Go to Top