/*
After 34 years of almost every programming-language there has been, I changed
to C++ by 1993 and was very happy with it until GNU's gcc -v 4.x.x. As
preparation to Android I started a home-made crash-course Java after I guess
80000 - 90000 hours of computing, last november.

I must say that I regret that I did not do so 10 years earlier, but there are
a few things I miss. One is the address operator (&), in Java one cannot see
where an item is stored in memory. Nor ( I believe ) how much memory remained
unused.

I learned that Strings cannot be overwritten nor deleted and made this test:

import java.io.*;

public class StringTest {

static String line;

public static void main(String[] args) {

line = "Peter is razy";
line = "Mary is a fat cow";

System.out.println(line);
} // main
} // StringTest

As expected: The java compiler does not flag an error on using one variable
twice, and the 'program' outputs last:

Mary is a fat cow

This means that the first, by not being overwritten nor deleted, still
occupies part of my memory until the garbage collector removes it to-night,
to-morrow, next month ( ? ).

The other disappointment on Java is that I cannot see how much memory remained
unused. If so, I could call names to the whole population of the town I live
in and know if my guess is right ( please tell me if I am wrong ).

/*
After 34 years of almost every programming-language there has been, I changed
to C++ by 1993 and was very happy with it until GNU's gcc -v 4.x.x. As
preparation to Android I started a home-made crash-course Java after I guess
80000 - 90000 hours of computing, last november.

I must say that I regret that I did not do so 10 years earlier, but there are
a few things I miss. One is the address operator (&), in Java one cannot see
where an item is stored in memory. Nor ( I believe ) how much memory remained
unused.

Learn to live without them.
Java is not about manipulating memory addresses.

Originally Posted by nieuwenhuizen-jk

I learned that Strings cannot be overwritten nor deleted and made this test:

import java.io.*;

public class StringTest {

static String line;

public static void main(String[] args) {

line = "Peter is razy";
line = "Mary is a fat cow";

System.out.println(line);
} // main
} // StringTest

As expected: The java compiler does not flag an error on using one variable
twice, and the 'program' outputs last:

Mary is a fat cow

This means that the first, by not being overwritten nor deleted, still
occupies part of my memory until the garbage collector removes it to-night,
to-morrow, next month ( ? ).

Not quite. Those Strings are hard coded there, and so stored in the String literal pool by the compiler. it is possible the compiler is smart enough to know that line is instantly redirected to the second String and so not store the first, but I don't know if that's the case. In any case, neither String (in this example) is ever eligible for garbage collection.

Originally Posted by nieuwenhuizen-jk

The other disappointment on Java is that I cannot see how much memory remained
unused. If so, I could call names to the whole population of the town I live
in and know if my guess is right ( please tell me if I am wrong ).

What is it you're trying to do?
That code example will potentially cause your system to throw an OOM because you are attempting to read in the entire phone directory and store it in memory.
Put simply, that's not how your code should be written.

Tell us what you;re trying to do with this phone directory and we'll tell you how it should be done.

Originally Posted by nieuwenhuizen-jk

And now that I contacted you: I do understand that a 'static' variable is one
which has that name in C++ also: one and the same value for all instances,
saving it once is sufficient.

But why is it that I cannot call a non-static variable from a static text, which
enforced me to make StringText.line static?

Thanks a lot

Nieuwenhuizen
2011-05-13T09:40 CEST

Static means the attribute is associated with the Class.
Normal attributes are associated with an instance of a class, so do not exist with there being an actual object. Consequently you cannot reference them from a static context because they do not exist in a static context.

I am afraid not. Sorry. The first line of class StringTest was "String line". `javac` gave me 3 identical errors:

"non-static variable line cannot be referenced from a static context" :
line = ....
^
line = ....
^
.....printline(line);
^
After adding 'static' before 'String line' it appeared to be accepted by `javac`, but I do not understand why. ( To be honest, that is my main problem with java ). Then I could finish my message.

I did not mention the word "phone directory", and what I sent was a simplified example.

I just need to load items described by one line for each in a textfile into an Array of Objects, constructed as Object(String). The object can be anything from spare bolts to enormous jet-engines.for AirBus 800.

I am afraid not. Sorry. The first line of class StringTest was "String line". `javac` gave me 3 identical errors:

"non-static variable line cannot be referenced from a static context" :
line = ....
^
line = ....
^
.....printline(line);
^
After adding 'static' before 'String line' it appeared to be accepted by `javac`, but I do not understand why. ( To be honest, that is my main problem with java ). Then I could finish my message.

Thanks for your time
N

I will restate.
Static (in Java) means "part of the Class".
So a static method does not require an instance of the class to be called, and similarly with a static attribute.

Since main() (by its very nature) is static, you cannot refer to anything non-static in a class without first creating an instance of that class.

I did not mention the word "phone directory", and what I sent was a simplified example.

I just need to load items described by one line for each in a textfile into an Array of Objects, constructed as Object(String). The object can be anything from spare bolts to enormous jet-engines.for AirBus 800.

OK.
But what you do with that effects how you should deal with that data.
If you have a million lines in your file then turning that into an array of a million objects is clearly a rubbish design.

As far as the OP goes, the 'main' method is not intended for class code - it is an entry point for execution. In the main method will typically be code to instantiate and test instances of the class, or, for an application control class, the code to instantiate the application.

Methods and fields that are static belong to the class type and are shared by all instance objects of that class. This allows type-specific data and actions to be implemented, such as a count of the number of instances created, or the declaration of type-specific constants. The main method is static for obvious reasons (when starting an application or testing, you need to create an instance from scratch).

You can access static data from any method, but a static method can only access static data unless explicitly given an instance object.