Saturday, October 27, 2018

Get Superclass


getSuperclass()

Class c = p.getClass();
while((c = objectReference.getSuperclass())!=null){
    System.out.println(c.getSimpleName());
}




Sunday, May 18, 2014

GitBash

.gitignore


git touch .gitignore

vi .gitignore

i (for insert)

type text

escape

:q to quit vi
:wq to save and quit vi

cat .gitignore to read .gitignore

other


.git add <name>
.git add -A (adds all in dir)

Thursday, April 3, 2014

Anonymous Classes

"Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/1hiO0OV )

Wednesday, March 26, 2014

Installing plugins in Eclipse

How to install plugins in Eclipse:

  • From the main menu > Help > Install new software > Add new
  • enter the following:
    • name: name of plugin (a description is fine)
    • url: url of the plugin's update site
  • click Add

Once the list of available software has populated, select the components you wish to install and click Next. Accept any licence agreements and click OK. Eclipse will require a restart to complete the plugin installation.

Monday, March 24, 2014

Variable scope and lifetime


The scope of a variable refers to how visible it is to different parts of the program. A variable which is declared inside of a method is said to have local scope - this means that it is visible only within that method. A variable which is declared at the top of a class is said to have class-level scope.

In the example below, a variable called username has been declared twice - each has a different scope. The one at the top has class-level scope and the one in the main method has local scope. The compiler will first attempt to reference the variable locally and if one cannot be found, it will look for one at class-level.

As a result, when the main method references the variable called username, it will be looking at the local declaration and therefore print "KiddThunda". When the variable called username is referenced from methodOne(), "Straven" is printed because it is referencing the variable at class-level.





In the next example, width and height variables are declared and assigned values in the initialiseValues() method. 



Because the width and height variables are local to the initialiseValues() method, they cannot be seen from the calculateArea() method - or any other method.

If you require access to a variable(s) from many methods, you can increase the scope of the variable to class-level, as below:


(In this case, because the variables are being called from static methods, the variables are declared with the static keyword - but this is just incidental and not particular to the discussion on scope.)

The scope of a variable has a direct affect on where it can be seen in the program. Additionally, the scope of a variable affects the lifetime of the variable.

Lifetime refers to the period of time that a variable stores a value before having it reset.

Consider the following method which is using the variable numberOfClicks to keep count of the number of clicks. Syntactically correct, however, the logic is incorrect. Each time the method is called, the numberOfClicks variable is being re-declared and set to zero before the variable is incremented. This results in the variable always having a value of 1. We can say that the numberOfClicks variable has a lifetime of 1 method call.



The correct logic is below:


The scope of numberOfClicks has been increased to class-level, therefore extending it's lifetime. Now, the variable is declared and initialised once. When the countClicks() method is called, numberOfClicks is actually incremented. The numberOfClicks variable now has the same lifetime as the object in which it is contained (Scope3Demo).

It is a common scenario that you declare variables locally and then realise that the scope and lifetime of some of them will need to be increased, as your project grows.

Variables may also have block scope - meaning, the are used within the block of a loop or conditional (if) block.

The loop counter in a for loop is a good example of this:


Notice that the loop counter (x) can be accessed within the loop block but not outside of it. We can say that x has a lifetime of the length of the loop.

In the example below, the error is occurring because the variable called message has block scope - that is, the variable has been declared within the conditional block and cannot be seen outside of that block.




We can increase the scope of message if we need to use it beyond the conditional block:


Why the main method is static

In Java, there must be a main method in at least one class. This is the front door to the application. The runtime system begins with a call to main and in turn, the main method calls other functions of the program.

The main method is a special case and is always declared with the static keyword - this means that the method belongs to the class rather than to an instance of the class - there is only one.

All internal members (attributes) and methods which are called by main, must have the static modifier.

If you attempt to call a non-static internal method from a static method, you will get a compile error, as in below:


Notice that the call to the doSomething() method does not have errors. This is because it has been defined with the static keyword. The call to doSomethingElse(), however, has errors because it is a non-static method.

Similarly, attempting to call a non-static member from the static method will produce compile errors:



Notice that the call to the username member does not have errors. This is because it has been defined with the static keyword. The call to password, however, has errors because it is a non-static member.

This is different to an instance (a regular object) where there may be many instances. In this case, members and methods belong to each instance and are not static.

Sunday, March 9, 2014

Convert a negative number to a positive number


Code sample on pastebin

"Math.abs (Java Platform SE 7 )" ( http://bit.ly/1hWJjy9 )

Consider the following example where we want to calculate the distance between 2 axis coordinates.

int x1 = 200;
int x2 = 300;
int distance = x1 - x2;

The result is distance =  -100.

If we are simply interested in the distance and not whether the distance is a negative, we can use Math.abs to get the absolute (positive) value of distance, like so:

int distance = Math.abs(x1 - x2);

The result is distance =  100.