|
You have to define wb2 before you can initialize it or set it's value to another object. So somehwere, you'll also need
SXSSFWorkbook wb2 = null;
Or you could just define it and initialize it to wb:
SXSSFWorkbook wb2 = wb;
Java won't automatically figure out what wb2 should be, if you haven't previously defined it, the Java compiler will just tell you that you have an undefined variable.
Craig
------- Original Message -------
On Friday, July 28th, 2023 at 10:50 PM, Peter Dow <petercdow@xxxxxxxxx> wrote:
Ah, I see. In the program I've been working on there's this:
SXSSFWorkbook wb = null;
which seems to satisfy both your points -- it sets the type for wb, and makes an explicit initialization to null so whoever is reading the program can see that it's starting off as null.
So what happens if there's something like this:
SXSSFWorkbook wb = null;
wb = new SXSSFWorkbook();
wb2 = wb;
What is the type of wb2 at this point? Is this even allowed? If it is allowed, I would sort of expect wb2 to be type SXSSFWorkbook.
On 7/28/2023 4:55 PM, Craig Pelkie wrote:
Peter
Off list again, since I don't think it will post to the list.
There are two parts of using a variable:
Definition
Initialization
Definition is when you specify the type of the variable:
int aNumber;
Font fontDetail;
In the int definition, aNumber is initialized to 0, the default for a "primitive type". Numeric and boolean definitions have default initialization values, although it is perfectly OK (and I prefer to do this) like so:
int aNumber = 0;
When you have a definition like this:
Font fontDetail;
You are defining an object named fontDetail of type Font, but you are not initializing it. Because it is not initialized and because Font is not a primitive type, the initial value of fontDetail is null. So if you try to use fontDetail at this point, you'll get a null object exception. You'll get a lot of those as you go along, part of the learning curve.
Null object exceptions tend to happen most frequently with String objects. For example if you have
String myName;
>From an RPG point of view, we'd think myName should be blank, but no, it is null, since myName is an object of type String, not a primitive type.
General rule:
Always initialize things as part of the definition, or very, very near to it in your code, so you can see it:
String myName = "";
I don't think your example will work:
wb = new SXSSFWorkBook();
If it does, it means that somewhere in the code is a definition statement:
SXSSFWorkbook wb;
Java needs to know the type of wb before it can assign a value to it.
The "new" keyword invokes the constructor method of SXSSFWorkbook, in this case, the constructor method that has no parameters (the "default constructor"). So the default constructor method probably has several statements that initialize the values of the properties that are defined inside SXSSFWorkbook, then it returns the initialized object to your code, assigning the initialized object to your wb object.
Craig
------- Original Message -------
On Friday, July 28th, 2023 at 6:38 PM, Peter Dow<petercdow@xxxxxxxxx> wrote:
Thanks Barbara. Between you, Craig Pelkie, and robmorning, I'm beginning
to get it.
If I define it within MyClass, it's similar to a module-level field in RPG.
If I define it within the main() method, it's similar to a
procedure-level field in RPG.
If I define it within an IF statement, it's not the same as anything in
RPG.
The other thing, I'm not certain of this yet, is what constitutes
defining a field/variable? At the moment, it looks like specifying the
type ahead of the field/variable name means I'm defining/declaring it,
and if I leave off the type, I'm using whatever version of the
field/variable was defined in the closest scope.
Also, a variable can be defined with the "new" keyword, e.g.
wb = new SXSSFWorkbook();
without specifying the type, since the type is determined by the
method's constructor method.
On 7/28/2023 2:55 PM, Barbara Morris wrote:
On 2023-07-27 6:56 p.m., Peter Dow wrote:--
I'm very much a beginner with Java and I'm having a problemIf you define a variable inside a block, it's only visible within that
understanding how scope works.
...
block.
Here, my variable "str" can't even be used in main() outside the "if"
block where I defined it.
class MyClass
{
static public void main()
{
if (1 == 2)
{
String str = "abc"; // only visible in this "if" block
str = "def"; // ok, still in the "if" block
}
str = "ghi"; // str is not visible any more
}
}
This is the Java Programming on and around the IBM i (JAVA400-L) mailing list
To post a message email:JAVA400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:https://lists.midrange.com/mailman/listinfo/java400-l
or email:JAVA400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
athttps://archive.midrange.com/java400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.