473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Static Vs. Private

It's been a while since I got down and dirty with some fashioned Java code....

I am wondering what's the difference between a static and a private method?

public class Myclass(){

public static void Main(String args[] str){




}

}

public class myobj(){
static int c1(){

Console.WriteLine("Hello!");

}

private void c2(){
Console.WriteLine("Made it here....");
}

}
Oct 8 '07 #1
5 2630
r035198x
13,262 8TB
It's been a while since I got down and dirty with some fashioned Java code....

I am wondering what's the difference between a static and a private method?

public class Myclass(){

public static void Main(String args[] str){




}

}

public class myobj(){
static int c1(){

Console.WriteLine("Hello!");

}

private void c2(){
Console.WriteLine("Made it here....");
}

}
1.) Use code tags when posting code.
2.) What do the textbooks/tutorials say about static and private?
3.) That code is C# not Java.
Oct 8 '07 #2
Ganon11
3,652 Expert 2GB
First, Please don't double post your questions.

A static method is one that does not depend on any object. The Math class is a perfect example. It is filled with static functions (like Math.random()) and static variables (like Math.PI) that you can use without ever creating a Math object.

A private method is one that outside classes don't need to see (and, in fact, shouldn't). It can only be called and be used by the object you are using. An example would be a helper function to initialize variables.

Suppose your class requires 3 member variables. You could have a constructor that forces the user to give all 3 variables when instantiating, or you could provide several different constructors taking different arguments and different amounts, letting your user specify only, say, 2 of these 3 values, and provide a default value for the 3rd.

Instead of writing 7 different sets of 'nearly' identical code for each constructor, write a private initialize() method that accepts those 3 arguments (which will either be user-defined values or default values) and does the work of initializing the variables.

Certainly, no other class should be able to call this method - we don't want them messing up the values of these member functions. It should only ever be called from the constructor of that class - so you make it private.
Oct 8 '07 #3
JosAH
11,448 Expert 8TB
I am wondering what's the difference between a static and a private method?
You're asking about two completely unrelated things; your question makes as
much sense as asking about the difference between nasal hair growth and the
efficiency of three herrings. Please rethink your question and then rephrase it.

kind regards,

Jos
Oct 8 '07 #4
r035198x
13,262 8TB
You're asking about two completely unrelated things; your question makes as
much sense as asking about the difference between nasal hair growth and the
efficiency of three herrings. Please rethink your question and then rephrase it.

kind regards,

Jos
... the efficiency of three herrings?
Oct 9 '07 #5
JosAH
11,448 Expert 8TB
... the efficiency of three herrings?
Yep, I want to know the difference between them and nasal hair growth ;-)

kind regards,

Jos
Oct 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
6
by: Matt | last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
5
by: TomislaW | last post by:
What is the purpose or difference between private static and private method in non-static class?
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.