-
private and static
A couple of newbie questions:
Intro to question #1...
I have never programmed in JAVA and I have inherited about 6000 lines of
JAVA code (all one class) called "public class GenerateReports". Within
this class there are 25 methods.
Question #1. Except for the one constructor method, why would all the other
methods (whether private or public) be static and void? I can understand
why a public method would be static and void, but if a private method is
only accessible within its own class why would you make it static?
Question #2. The code in question is as follows:
public static void main(String args[]) {
if (2 != args.length) {
System.err.println("Usage: java GenerateReports dataSourceName initYear");
System.exit(0);
}
GRInstance = new GenerateReports(); //create an instance of Generate
Reports so we can read the properties file.
SCHEMA = DATA_SOURCE_NAME = args[0];
int initYear = -1;
--------------------------------------
My question is with the line "SCHEMA = DATA_SOURCE_NAME = args[0];"
From what I can tell main is being passed 2 arguments - dataSourceName and
initYear. First Java checks to make sure both those arguments have been
passed and if not it prints out an error message. Next it creates an instance
of Generates Reports - a constructor. Then I get lost. The next statement
looks like an declaration and assignment statement but I can't figure it
out. Is SCHEMA a type? If so I can not find any documentation in my JAVA
book.
Sorry for the long question, and thanks alot for the help.
-
Re: private and static
Hi Mark,
From your questions I guess that you are quite new to Java Programming
language.Well , I am trying to answer to your question as easy and understandable
as possible.
For Question# 1
Static methods are meant to be accessed just by class name ie, even if you
don't have an instance of that class.If a static method is accessing another
method of the class then that method should also be a static methos logically.
right? Also that if the author need not want the method to be exposed outside
then he can declare it as private. So in your case the author has declared
a private method to separate out some functionality done by a static method
, which need not be accessed by outside world.
For Question#2
In Java you can do assign a common value to many variables using the syntax
you are seeing there.
suppose there are 2 variables
int a;
int b;
// If you want to assign value 2 to both a and b then you do as
a = 2;
b = 2;
also you can do a = b = 2;
in Java.
In your case check the class file thoroughly. The variables
SCHEMA and DATA_SOURCE_NAME must be defined some where above.
They are trying to assign the value of the first argument args[0]to two variables
SCHEMA and DATA_SOURCE_NAME at the same time.
Hope This helps.
Bala.
"Mark" <msorteberg@inspec.com> wrote:
>
>A couple of newbie questions:
>Intro to question #1...
>
>I have never programmed in JAVA and I have inherited about 6000 lines of
>JAVA code (all one class) called "public class GenerateReports". Within
>this class there are 25 methods.
>
>Question #1. Except for the one constructor method, why would all the other
>methods (whether private or public) be static and void? I can understand
>why a public method would be static and void, but if a private method is
>only accessible within its own class why would you make it static?
>
>Question #2. The code in question is as follows:
>
>public static void main(String args[]) {
>
> if (2 != args.length) {
> System.err.println("Usage: java GenerateReports dataSourceName initYear");
> System.exit(0);
> }
>
> GRInstance = new GenerateReports(); //create an instance of Generate
>Reports so we can read the properties file.
>
> SCHEMA = DATA_SOURCE_NAME = args[0];
> int initYear = -1;
>--------------------------------------
>My question is with the line "SCHEMA = DATA_SOURCE_NAME = args[0];"
>
>From what I can tell main is being passed 2 arguments - dataSourceName and
>initYear. First Java checks to make sure both those arguments have been
>passed and if not it prints out an error message. Next it creates an instance
>of Generates Reports - a constructor. Then I get lost. The next statement
>looks like an declaration and assignment statement but I can't figure it
>out. Is SCHEMA a type? If so I can not find any documentation in my JAVA
>book.
>
>Sorry for the long question, and thanks alot for the help.
>
-
Re: private and static
bala:
Thanks for your response. Let me now take it to the next level.
-Java has CLASSES
-Within a CLASS you can have METHODS
-Those methods can either be PUBLIC or PRIVATE
-Java has a keyword called STATIC
-STATIC can be applied to VARIABLES and/or METHODS
-The application of the word STATIC to a VARIABLE makes it "global". It
becomes a CLASS variable not an INSTANCE variable.
Now for the clincher
-If the above is true, then if one assigns the keyword STATIC to a method
doesn't that make the method "global"? Or, is the inclusion or omission of
the keyword STATIC from a method declaration simply JAVA's way of saying
(if the STATIC keyword is present in the definition), "Hey! This method
is a "class" method." Or, if the keyword STATIC is not present, "Hey! This
method is an instance of a class or method."
Thanks everyone for your patience with me.
====================================
"bala" <yawler@hotmail.com> wrote:
>
>Hi Mark,
> From your questions I guess that you are quite new to Java Programming
>language.Well , I am trying to answer to your question as easy and understandable
>as possible.
>
>For Question# 1
>
>Static methods are meant to be accessed just by class name ie, even if you
>don't have an instance of that class.If a static method is accessing another
>method of the class then that method should also be a static methos logically.
>right? Also that if the author need not want the method to be exposed outside
>then he can declare it as private. So in your case the author has declared
>a private method to separate out some functionality done by a static method
>, which need not be accessed by outside world.
>
>For Question#2
>
>In Java you can do assign a common value to many variables using the syntax
>you are seeing there.
>
>suppose there are 2 variables
>
>int a;
>int b;
>
>// If you want to assign value 2 to both a and b then you do as
>
>a = 2;
>b = 2;
>
>also you can do a = b = 2;
>in Java.
>
>In your case check the class file thoroughly. The variables
>SCHEMA and DATA_SOURCE_NAME must be defined some where above.
>They are trying to assign the value of the first argument args[0]to two
variables
>SCHEMA and DATA_SOURCE_NAME at the same time.
>
>Hope This helps.
>Bala.
>
>"Mark" <msorteberg@inspec.com> wrote:
>>
>>A couple of newbie questions:
>>Intro to question #1...
>>
>>I have never programmed in JAVA and I have inherited about 6000 lines of
>>JAVA code (all one class) called "public class GenerateReports". Within
>>this class there are 25 methods.
>>
>>Question #1. Except for the one constructor method, why would all the other
>>methods (whether private or public) be static and void? I can understand
>>why a public method would be static and void, but if a private method is
>>only accessible within its own class why would you make it static?
>>
>>Question #2. The code in question is as follows:
>>
>>public static void main(String args[]) {
>>
>> if (2 != args.length) {
>> System.err.println("Usage: java GenerateReports dataSourceName initYear");
>> System.exit(0);
>> }
>>
>> GRInstance = new GenerateReports(); //create an instance of Generate
>>Reports so we can read the properties file.
>>
>> SCHEMA = DATA_SOURCE_NAME = args[0];
>> int initYear = -1;
>>--------------------------------------
>>My question is with the line "SCHEMA = DATA_SOURCE_NAME = args[0];"
>>
>>From what I can tell main is being passed 2 arguments - dataSourceName
and
>>initYear. First Java checks to make sure both those arguments have been
>>passed and if not it prints out an error message. Next it creates an instance
>>of Generates Reports - a constructor. Then I get lost. The next statement
>>looks like an declaration and assignment statement but I can't figure it
>>out. Is SCHEMA a type? If so I can not find any documentation in my JAVA
>>book.
>>
>>Sorry for the long question, and thanks alot for the help.
>>
>
-
Re: private and static
Mark,
I'm a newbee myself..been busy with Java for 7 weeks now, but it's mainly
like this:
Private and static are two totally different things. If you declare a method
private it only means that that method can only be accessed from inside the
class where that private method is defined and not in any other classes in
the package let alone outside the package.
Declaring a method Static means that you can use that method without first
having to make an object that the mthod refers to.That is in fact the important
thing about static methods..they don't refer to any object and thus you can
use them without making an object first.
Suppose you have the following class
class NonStatic{
void prt (String s){
System.out.println(s);}
}
Now if you wanna use that prt method you have to make an object NonStatic
first:
NonStatic ns = new NonStatic();
ns.prt();
As opposed to the following static method:
class WithStaticMethod{
static void prt (String s){
System.out.println(s);}
}
Now if I want to use the prt method to print something to the screen I can
call that method directly through it's class, like this:
WithStaticMethod.prt("something I wanna print");
The ability to do this is essential in the main method of a program. When
starting up the program I need to have a main method that is static. The
program is a total of objects that I can't make/instantiate if the program
isn't running. Your program is like a house with a lot of doors in them.
you can open these doors (make an object) by opening the door with a key.These
keys hang on the wall in the hallway. The frontdoor must thus be open,because
what could you do if you needed a key for that too,but they are all inside
the house? So it's essential that the main method is static always.
Hope this helps any )
Patrick
"Mark" <msorteberg@inspec.com> wrote:
>
>bala:
>
>Thanks for your response. Let me now take it to the next level.
>
>-Java has CLASSES
>-Within a CLASS you can have METHODS
>-Those methods can either be PUBLIC or PRIVATE
>
>-Java has a keyword called STATIC
>-STATIC can be applied to VARIABLES and/or METHODS
>-The application of the word STATIC to a VARIABLE makes it "global". It
>becomes a CLASS variable not an INSTANCE variable.
>
>Now for the clincher
>-If the above is true, then if one assigns the keyword STATIC to a method
>doesn't that make the method "global"? Or, is the inclusion or omission
of
>the keyword STATIC from a method declaration simply JAVA's way of saying
>(if the STATIC keyword is present in the definition), "Hey! This method
>is a "class" method." Or, if the keyword STATIC is not present, "Hey!
This
>method is an instance of a class or method."
>
>Thanks everyone for your patience with me.
>
>====================================
>
>"bala" <yawler@hotmail.com> wrote:
>>
>>Hi Mark,
>> From your questions I guess that you are quite new to Java Programming
>>language.Well , I am trying to answer to your question as easy and understandable
>>as possible.
>>
>>For Question# 1
>>
>>Static methods are meant to be accessed just by class name ie, even if
you
>>don't have an instance of that class.If a static method is accessing another
>>method of the class then that method should also be a static methos logically.
>>right? Also that if the author need not want the method to be exposed outside
>>then he can declare it as private. So in your case the author has declared
>>a private method to separate out some functionality done by a static method
>>, which need not be accessed by outside world.
>>
>>For Question#2
>>
>>In Java you can do assign a common value to many variables using the syntax
>>you are seeing there.
>>
>>suppose there are 2 variables
>>
>>int a;
>>int b;
>>
>>// If you want to assign value 2 to both a and b then you do as
>>
>>a = 2;
>>b = 2;
>>
>>also you can do a = b = 2;
>>in Java.
>>
>>In your case check the class file thoroughly. The variables
>>SCHEMA and DATA_SOURCE_NAME must be defined some where above.
>>They are trying to assign the value of the first argument args[0]to two
>variables
>>SCHEMA and DATA_SOURCE_NAME at the same time.
>>
>>Hope This helps.
>>Bala.
>>
>>"Mark" <msorteberg@inspec.com> wrote:
>>>
>>>A couple of newbie questions:
>>>Intro to question #1...
>>>
>>>I have never programmed in JAVA and I have inherited about 6000 lines
of
>>>JAVA code (all one class) called "public class GenerateReports". Within
>>>this class there are 25 methods.
>>>
>>>Question #1. Except for the one constructor method, why would all the
other
>>>methods (whether private or public) be static and void? I can understand
>>>why a public method would be static and void, but if a private method
is
>>>only accessible within its own class why would you make it static?
>>>
>>>Question #2. The code in question is as follows:
>>>
>>>public static void main(String args[]) {
>>>
>>> if (2 != args.length) {
>>> System.err.println("Usage: java GenerateReports dataSourceName initYear");
>>> System.exit(0);
>>> }
>>>
>>> GRInstance = new GenerateReports(); //create an instance of Generate
>>>Reports so we can read the properties file.
>>>
>>> SCHEMA = DATA_SOURCE_NAME = args[0];
>>> int initYear = -1;
>>>--------------------------------------
>>>My question is with the line "SCHEMA = DATA_SOURCE_NAME = args[0];"
>>>
>>>From what I can tell main is being passed 2 arguments - dataSourceName
>and
>>>initYear. First Java checks to make sure both those arguments have been
>>>passed and if not it prints out an error message. Next it creates an
instance
>>>of Generates Reports - a constructor. Then I get lost. The next statement
>>>looks like an declaration and assignment statement but I can't figure
it
>>>out. Is SCHEMA a type? If so I can not find any documentation in my
JAVA
>>>book.
>>>
>>>Sorry for the long question, and thanks alot for the help.
>>>
>>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks