Don't think I totally understand methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Don't think I totally understand methods

what do public static and void all mean for the program?

23rd Jun 2017, 5:02 PM
Stephen Delarosa
Stephen Delarosa - avatar
3 Answers
0
theres 2 type of functions: 1》void : used for texts void main(){ } 2》int for numbers and must have at end return int main (){ return 0; //or values } public make you getting information from others class's or functions
23rd Jun 2017, 5:28 PM
Elie Douaihy
Elie Douaihy - avatar
0
Let's start with the Functions and Methods. Each Function/Method has a Return Type. That means that when this Function or method is being run it holds a value to it. Example: String x = Integer.toString(1); In this case the .toString(int); is a function that returns a String value. The basic different types of return types are: -String -char -int -boolean -void There are other return types as well but stick to the basics for now. Remember: void does not return a value that the programmer can use. Next are Public and Private: When you're going to start working with classes you're going to find that in every example they use public and private for certain variables or functions. Public makes a function or variable available outside the class and Private makes a function or variable inaccessible from outside the class, but can still be used inside the class. This is good for various reasons. First, you as a programmer have a cleaner code and makes everything a lot more readable. Second, it avoids the user or programmer from interfering with the class's variables or functions. Static: Static claims that a function or variable is available even if an instance of the class is not created. That means that I can create a class and without having to fill in addition values or variables I can directly call the method, function or variable from the class. Example: class Dog{ public static String type = "K9"; public String name; private int Age = 5; } Print(Dog.type); Output would be "K9" Because it is static print(Dog.name); Output would be an error. Because it is not static, thus there's an error Dog myDog = new Dog(); myDog.name = "Ben"; print(myDog.name); Output would be "Ben" I created an instance of Dog, and accessed it's name variable, which is public. print(myDog.Age); output would be error. Because Age is not public and can not be accessed from outside the class
23rd Jun 2017, 6:34 PM
Limitless
Limitless - avatar
0
Java is just a collection of classes. Inside a class, you'll find methods, variables, etc.. if a method is public you can access it outside that class, the same with variables. But if the method/variable is private you can only access it inside that class. see Limitless examples... void means doesn't return anything(String, int, boolean, etc...). it doesn't return information for other class to be processed, however it can take some arguments and process it base on what the method does. example. class A{ static void Sum(int a, int b){ System.out.println(a+b); } public static void main(String[] args){ Sum(4,5); } }
24th Jun 2017, 12:45 AM
Mr. Yoh So
Mr. Yoh So - avatar