What's new

Closed Question about method/function

Status
Not open for further replies.

Princess Serrano

Eternal Poster
Joined
Apr 2, 2016
Posts
429
Reaction
285
Points
285
Hello java expert

Ask lang po about if pede maging parameter ang void.

Let say

public void start(void e) {
new Thread(new Runnable() {
e();
}).start();
}

natry ko na po yan kaso ayaw e. Help naman thanks.
 
Kindly check this one:
You do not have permission to view the full content of this post. Log in or register now.

Pwede kang gumamit ng void as parameter, pero once na cinall mo ang method, dapat null ang value na ipapasa mo sa kanya, otherwise mag ge-generate ito ng error.

Based sa example mo, cinall mo ang parameter na "e" which is method, mag ge-generate yan ng error kasi ang parameter mo ay void which is nag a-accept lang ng null values.
 
I dont know Java's coding rules but void is a reserved word which you cannot utilize for naming a variable or any of a kind of use.
 
Kindly check this one:
You do not have permission to view the full content of this post. Log in or register now.

Pwede kang gumamit ng void as parameter, pero once na cinall mo ang method, dapat null ang value na ipapasa mo sa kanya, otherwise mag ge-generate ito ng error.

Based sa example mo, cinall mo ang parameter na "e" which is method, mag ge-generate yan ng error kasi ang parameter mo ay void which is nag a-accept lang ng null values.

ibig mo pong sabihin sir, ang Void is similar to String, int and double. Pero ang return dapat ay null? Pano niya icocall yung method na gusto ko pong icall? For example.

public void threadCenter(Void function) {
new Thread(new Runnable() {
public void run() {
function();
}
}).start();
}

public Void function() {
System.out.println("Hello");
return null;
}

ganyan po ba?
 
ibig mo pong sabihin sir, ang Void is similar to String, int and double. Pero ang return dapat ay null? Pano niya icocall yung method na gusto ko pong icall? For example.

public void threadCenter(Void function) {
new Thread(new Runnable() {
public void run() {
function();
}
}).start();
}

public Void function() {
System.out.println("Hello");
return null;
}

ganyan po ba?
void is not a You do not have permission to view the full content of this post. Log in or register now.. However, void is a valid You do not have permission to view the full content of this post. Log in or register now. used to indicate that a You do not have permission to view the full content of this post. Log in or register now..
Hindi siya data type, pero ginagamit siya to indicate that the function must not return a value.

Sa example mo alisin mo sa loob ng function() ang return keyword, kasi based sa method declaration mo naka void which is not returning a value.

Try this code kung mag wo-work
Code:
public void threadCenter(Void function) {
new Thread(new Runnable() {
public void run() {
function();
}
}).start();
}

public Void function() {
System.out.println("Hello");
}
 
Hindi siya data type, pero ginagamit siya to indicate that the function must not return a value.

Sa example mo alisin mo sa loob ng function() ang return keyword, kasi based sa method declaration mo naka void which is not returning a value.

Try this code kung mag wo-work
Code:
public void threadCenter(Void function) {
new Thread(new Runnable() {
public void run() {
function();
}
}).start();
}

public Void function() {
System.out.println("Hello");
}



code below return two "Hello"
I guess po everytime na nalalagay si Void sa line of code, nag eexecute siya automatic. Pero di padin nameet yung gusto kong mangyare na maging parameter ang void.



public class Main {
public void threadCenter(Void function) {
new Thread(new Runnable() {
public void run() {
function();
}
}).start();
}

public Void function() {
System.out.println("Hello");
return null;
}

public static void main(String... args) {
Main m = new Main();
m.threadCenter(m.function());
}
}
 
code below return two "Hello"
I guess po everytime na nalalagay si Void sa line of code, nag eexecute siya automatic. Pero di padin nameet yung gusto kong mangyare na maging parameter ang void.



public class Main {
public void threadCenter(Void function) {
new Thread(new Runnable() {
public void run() {
function();
}
}).start();
}

public Void function() {
System.out.println("Hello");
return null;
}

public static void main(String... args) {
Main m = new Main();
m.threadCenter(m.function());
}
}
Hindi mo nga need ng return value kapag void ang method declaration mo.

Void is a keyword used in many programming languages. In JAVA, void is used when you do not want your function to return anything.

In Java, while defining a method, you usually define following three things with it :
1. Access specifiers such as public, protected.
2. Return type of the method.
3. Parameters to be passed in the function and their types.


The return type of a method tells what kind of value a method is supposed to return to the point from where it is called.

However, if you use void as the return type of a function, it doesn't need to return anything.

It just performs the work written through the code, and returns the control back to point from where it was called.

You do not have permission to view the full content of this post. Log in or register now.
 
Hindi mo nga need ng return value kapag void ang method declaration mo.

Void is a keyword used in many programming languages. In JAVA, void is used when you do not want your function to return anything.

In Java, while defining a method, you usually define following three things with it :
1. Access specifiers such as public, protected.
2. Return type of the method.
3. Parameters to be passed in the function and their types.


The return type of a method tells what kind of value a method is supposed to return to the point from where it is called.

However, if you use void as the return type of a function, it doesn't need to return anything.

It just performs the work written through the code, and returns the control back to point from where it was called.

https://phcorner.net/#forbidden#/What-is-the-meaning-of-void-in-java

nagalit kana kuya. Hahahahaha.


So paano ko po makocall yung method within method.
Ang gusto ko po kasi mangyari, maging parameter yung method tapos within method, icocall ko nalang siya.

For example

void sayHello
void sayHi

void executor(void script) {
script();
}

m.executor(sayHello());
m.executor(sayHi());


Gets po?
 
nagalit kana kuya. Hahahahaha.


So paano ko po makocall yung method within method.
Ang gusto ko po kasi mangyari, maging parameter yung method tapos within method, icocall ko nalang siya.

For example

void sayHello
void sayHi

void executor(void script) {
script();
}

m.executor(sayHello());
m.executor(sayHi());


Gets po?
Hindi naman ako galit nagpapaliwanag lang hahahaha.

Anyways tagal ko na hindi nag java, baka kupas na ako, testing mo to.

public class Main {
public void threadCenter(newClass functionObj) {
new Thread(new Runnable() {
public void run() {
functionObj.function();
}
}).start();
}

public static void main(String... args) {
Main m = new Main();
NewClass n = new NewClass();
m.threadCenter(n);
}
}

public class newClass{

public void function() {
System.out.println("Hello");
}
}
 
Hindi naman ako galit nagpapaliwanag lang hahahaha.

Anyways tagal ko na hindi nag java, baka kupas na ako, testing mo to.

public class Main {
public void threadCenter(newClass functionObj) {
new Thread(new Runnable() {
public void run() {
functionObj.function();
}
}).start();
}

public static void main(String... args) {
Main m = new Main();
NewClass n = new NewClass();
m.threadCenter(n);
}
}

public class newClass{

public void function() {
System.out.println("Hello");
}
}

Hi kuya! Thanks po. Gets ko na. Instead of calling a void, call a class nalang. Tapos yung class, may method dun. Kaso ang daming class kung ganun. Pero thanks po.
 
Hi kuya! Thanks po. Gets ko na. Instead of calling a void, call a class nalang. Tapos yung class, may method dun. Kaso ang daming class kung ganun. Pero thanks po.
Yup, you got my point, ang pagkakaalam ko kasi sa java hindi ka pwede mag declare ng function/method as parameter, kasi hindi siya data type. Unlike sa class consider as data type siya.

Pwede rin ganto ang declaration mo sa main:
public static void main(String... args) {
Main m = new Main();
m.threadCenter(new NewClass());
}

Instead na ganto:
public static void main(String... args) {
Main m = new Main();
NewClass n = new NewClass();
m.threadCenter(n);
}

Mas makakatipid ka ng one line.
 
Status
Not open for further replies.

Similar threads

Back
Top