Re: Beginner question referring to an object in a class from within method in that class

Peter Dow <[email protected]> Sun, 30 Jul 2023 10:43:31 -0700
Newsgroups gmane.comp.lang.as400.java
Message-ID <[email protected]>
I'm sure I would've run into that :)

On 7/29/2023 10:49 PM, Craig Pelkie wrote:
> 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 
> <[email protected]> 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<[email protected]>  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 problem
>>>>>> understanding how scope works.
>>>>>> ...
>>>>> If you define a variable inside a block, it's only visible within that
>>>>> 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:[email protected]
>>>> To subscribe, unsubscribe, or change list options,
>>>> visit:https://lists.midrange.com/mailman/listinfo/java400-l
>>>> or email:JAVA400-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org
>>>> Before posting, please take a moment to review the archives
>>>> athttps://archive.midrange.com/java400-l.
>>
>
-- 
This is the Java Programming on and around the IBM i (JAVA400-L) mailing list
To post a message email: [email protected]
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/java400-l
or email: JAVA400-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org
Before posting, please take a moment to review the archives
at https://archive.midrange.com/java400-l.