Private Static Int

Private Static Int



⚡ ALL INFORMATION CLICK HERE 👈🏻👈🏻👈🏻

































Private Static Int


Sign up with email
Sign up




Sign up with Google



Sign up with GitHub



Sign up with Facebook




Asked
3 years, 11 months ago


Active
3 years, 9 months ago


83.9k 19 19 gold badges 160 160 silver badges 245 245 bronze badges



Well you can't make it final because you need to alter the value when you count the lines.

–  markspace
Feb 5 '17 at 22:55






Well, I was thinking of saving the return value as the final..........

–  user6902338
Feb 5 '17 at 23:23



You can't do that either unless you initialize it that way, which would be bizarre in this case. If you have a method that returns the value you don't also need a variable containing the same value. What's the actual problem you're trying to solve?

–  user207421
Feb 5 '17 at 23:56



I am trying to create an instance variable that contains this method's output.

–  user6902338
Feb 6 '17 at 0:04



1. If you're creating a static variable, then it's NOT an instance variable, it's a class variable. 2. Re. "saving the return value" what EJP said: using code that accesses IO and files in a static initializer is terrible practice and you shouldn't do it. Just make a regular ol' static variable (not final) and you're done. Easy peasy.

–  markspace
Feb 6 '17 at 0:18


83.9k 19 19 gold badges 160 160 silver badges 245 245 bronze badges

JPMorgan Chase Bank, N.A. Moscow, Russia
Join G2i as a 100% Remote Node Engineer | Fully Remote Position | G2i Collective
Cleverbit Software San Gwann, Malta
Booming Games Malta Ltd. No office location
Java Engineer (m/f/x) onsite or remote (in Germany)
Scalable Capital GmbH München, Deutschland
Senior Software Engineers, R&D - Work in an open source company!

Stack Overflow

Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness


Products

Teams
Talent
Advertising
Enterprise



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us



Stack Exchange Network

Technology
Life / Arts
Culture / Recreation
Science
Other


Join Stack Overflow to learn, share knowledge, and build your career.
First things first... I am new at java and have only very recently begun experimenting with multi-class and multi-method code.
So, I have a program that I have to write that takes the grades of students in a class (read from an external file) and then calculate the final grade for each student. I am currently writing the bit of code that determines how many students are in the class. This involves counting the number of lines in the external file.
I have decided that I want to save the number of lines/number of students as a private static final int, (the current classification is just a placeholder). I am just not quite sure how to save it as such. In all of the examples, private static final(s) are declared like this:
tldr;
I am more than sure that this has been covered many times already, but what is the protocol for defining a public static final using a method?
You have misunderstood what static final is for, it's only for cases where a value can be set at the time that the class is loaded, and never changes after that. It's useful for declaring constants (like PI in your example) where you know what it should hold before the program ever runs.
Instead, for each line you read in, create a Student and add it to a list. Whenever you want the number of students you can call the size method on the list.
If you are really attached to the idea of making something static final, you could declare the List that way. The reference that the static final variable contains never changes but you can add to and modify its contents.
Cramming the file-reading code into the Student class as an instance method seems confused, you should find a better place for it. Reading all of the students from a file is not logic that belongs to an individual Student. Java isn't class-oriented or method-oriented, it's object-oriented. Put code in methods so they will be relevant to the instance you call them on.
1) Each line in the file "Prog349f.txt" represents the grades of each student. So N lines mean N student in the class.
2) You want to store this number of student in a private static final variable.
1) You can define getStudentNumber() as static (logic : the associated file is a class level file that contains grades of all students in the class and because the file is class level, the method reasonably can be declared as static).
2) And then call getStudentNumber() in the main class or any other class where you wish to save this number as private static final variable.
3) However, the implementation efficiency of your case study can be improved a lot. The way you have implemented this may not be the most perfect one. This will require careful analysis of the case.

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa . rev 2021.2.2.38474


initialization - How to initialize private static ... - Stack Overflow
How to declare a private static final int from a method... - Stack Overflow
object oriented - Why have private static methods? - Software...
How to get and set private static final field using... | Java: How To Do It
Private class fields - JavaScript | MDN | Private static methods

The best answers are voted up and rise to the top


Asked
6 years, 10 months ago


7,043 4 4 gold badges 25 25 silver badges 45 45 bronze badges


939 1 1 gold badge 7 7 silver badges 6 6 bronze badges



Yes, there's a reason. Your public/default static methods can still call your private statics. Whether static methods should be used at all, and when it's appropriate to use private methods of any kind, are separate questions so be careful not to mix them with this question.

–  user39685
Apr 1 '14 at 14:19






Quick example - factory method used by an inner class preventing invocation by other classes (you've gotta go through the factory method to get an instance of the class).

–  user40980
Apr 1 '14 at 14:21



@MattFenwick: you should post this as an answer.

–  Doc Brown
Apr 1 '14 at 14:23



By making a method static, you are also saying that it does not read or write to instance variables. A method that doesn't interact with instance variables is often easier to move and refactor to other places.

–  Mark Rogers
Apr 1 '14 at 15:56



Don't forget that regardless of whether a static method is private, internal or public, it still isn't thread-safe without specific code synchronization effort on your part.

–  Craig
Apr 1 '14 at 19:46


2,175 14 14 silver badges 17 17 bronze badges



I think you're on the right track, but I also think you're missing a key point. Most static methods have zero side effects (they're effectively functions, not methods) - that's why they're made static in the first place. The argument against them being private is "If they have no side effects, why not make them public to promote code reuse". That's the key point: when we make it private, it's usually because we want you to reuse the entire class that uses that private method, not just that method.

–  corsiKa
Apr 2 '14 at 17:32



I must say I have never done that or seen that behaviour. The few times I have done it was for helper methods to public static classes.

–  Miyamoto Akira
Apr 3 '14 at 22:06



@corsiKa: Side-effects aren't the issue. Making a static member of a class non-private effectively promises that every future version of a class will include that same method of the same name which does the same thing. If needs change, and a future version of the class may have no need for a method which works exactly like the current one does, a private method may safely be replaced with one that better fulfills the new needs of the class. As a simple example, suppose a class needs a method which takes a string and performs substitutions like < to < , etc. When it is written...

–  supercat
Aug 29 '14 at 17:27



...the strings which it is passed are known not to contain any ampersands, and it thus does not convert & to & [were I writing the code, I'd convert & to & even if I didn't expect it to be necessary, but suppose it doesn't]. It it later becomes necessary to handle ampersands, but the method is public, changing it to expand & to & could break outside code which performs its own & -to- & substitution. If the method is private, however, it could be fixed to handle & without causing problems for any outside code.

–  supercat
Aug 29 '14 at 17:32



That's an interesting problem to say the least, but I don't see how it being static makes it any more of a problem than not being static, which is key to this issue. If we follow your logic, everything everywhere would reimplement everything because "omg, what if there's a bug or requirements change someday?"

–  corsiKa
Aug 29 '14 at 17:38


1,232 8 8 silver badges 12 12 bronze badges



This is even more valuable when you need to pass derived (calculated) arguments to a superclass' constructor. The super(...) call must be the first line of your class' constructor, so you can't declare local variables to help you work out what to pass to the super call. However, you can call (private) static methods, which use as many lines as is necessary to work out the value to pass to the super constructor.

–  Andrzej Doyle
Apr 2 '14 at 7:25



note that there's the possibility of a static initializer block

–  Franz Ebner
Apr 2 '14 at 15:40


21.8k 5 5 gold badges 56 56 silver badges 65 65 bronze badges


4,484 16 16 silver badges 27 27 bronze badges



I like this answer, but do you have any up to date reference for this? "and maybe even a little faster"

–  Magnilex
Sep 23 '15 at 6:56



@Magnilex, The "this" pointer does not get passed to static class methods, therefore they will tend to be faster then static instance methods, unless the compiler detects the "this" is not used in the instance method (unlikely).

–  Ian
Sep 23 '15 at 17:14


6,220 1 1 gold badge 14 14 silver badges 34 34 bronze badges



"perhaps because it shares data between all instances of your class", that would be a static variable, not a method

–  Edson Medina
Jan 6 '20 at 17:55


6,611 1 1 gold badge 19 19 silver badges 47 47 bronze badges


6,439 5 5 gold badges 33 33 silver badges 53 53 bronze badges


Highly active question . Earn 10 reputation in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.


Cleverbit Software San Gwann, Malta
Backend Software Engineer, Creator Tooling
JPMorgan Chase Bank, N.A. Moscow, Russia
Full-Stack Engineer: Special Projects

Software Engineering

Tour
Help
Chat
Contact
Feedback
Mobile
Disable Responsiveness


Company

Stack Overflow
For Teams
Advertise With Us
Hire a Developer
Developer Jobs
About
Press
Legal
Privacy Policy
Terms of Service



Stack Exchange Network

Technology
Life / Arts
Culture / Recreation
Science
Other


Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility?
I would have thought an advantage to having a static method is that it can be called without an instance of a class, but since its private is there even a point to it being static?
The only reason I can think of is that it helps conceptually understanding the method on the class level as opposed to object level.
The characteristic of being static is independent of the visibility.
The reasons that you will want to have a static method (some code that does not depend on non-static members) will still be useful. But maybe you don't want anyone/anything else to use it, just your class.
A fairly common reason (in Java) would be for initializing immutable field variables in a constructor by using a simple private static method to reduce constructor clutter.
A somewhat contrived example follows...
1 Assuming it has no interaction with other static variables.
A common use-case for a private static method is a utility method which is
You see that you have a few lines of code that is repeated in a lot of your methods, so you decide to extract them to a single method, as duplicated code is not good.
You make the method private as it is not designed for widespread usage and you don’t want unrelated code calling it. (Debate this point in the comments….)
As the method does not access any instance fields, it can be make static, by making it static you make it easier to understand and maybe even a little faster.
Then.... (Maybe now, maybe later, maybe never)
Once the method has been made static, it is clear that it can be moved out of the class, say to an unity class.
It is also easy to transform it into an instance method of one of its parameters, often this is where the code should be.
I can think of at least two reasons why you would need a static private method on a class.
1: Your instances have reason to call a static method you don't want called directly, perhaps because it shares data between all instances of your class.
2: Your public static methods have subroutines that you don't want called directly. The method is still called without an instance, just not directly.
Of course, "it helps the class make sense" is a fine reason all on its own.
Generally, if I find I'm writing private static methods, I take it as an indication that there's something I should have modeled separately.
Since they're not tied to the state of a particular object instance, a collection of public and private static methods could form an entirely separate class with its own semantics & non-static methods.
(Another tip-off is if I find I have a lot of static methods (public and private) that all have a common parameter of some type, they might be better off as members of that type.)
So, to answer your question, private static methods appear when a class provides a group of related methods that are independent of an instance of that class. (...and since they're independent, they may be better off in their own class.)
Static is always better than non-static and worse than a free functions, which is worse than a pure function.
Private is always better than protected which is better than public.
Very simple example I can think of is, if you want to do some processing on input arguments (or some manipulation) which are passed to main function.
So in this case if processing is big and same functionality will not be used anywhere else it will make sense to have a private function as it will not be used/called from anywhere else + static as main is static.
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa . rev 2021.2.2.38474


Homemade Threesome Dp
Penetration 13
Overwatch Widowmaker Hot
Overwatch Rule 34 Porn
Hard Crush Before Sex

Report Page