I am a first year comp sci student and going through my first round of textbook problems, all dealing with the System.out.println method (no, im not looking for help on homework problems. i've satisfied (as far as i know right now) the requirements of the problem, am just looking to gain some extra information).
The first problem asked me to write a program that would output this:
//////////////////////
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
This was no problem. I wrote the following code:`
public class Stewie {
public static void main(String[] args) {
line();
qoute();
line2();
}
public static void line() {
System.out.println("//////////////////////");
}
public static void qoute() {
System.out.println("|| Victory is mine! ||");
}
public static void line2() {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}`
Later in the text, it asks me to write a program that prints the above figure 5 times in a row. This is also not a problem, I just rewrote the code from the first problem, like this:
/*
*/
public class Stewie2 {
public static void main(String[] args){
newStewie();
newStewie();
newStewie();
newStewie();
newStewie();
}
public static void newStewie() {
line();
qoute();
line2();
}
public static void line(){
System.out.println("//////////////////////");
}
public static void qoute(){
System.out.println("|| Victory is mine! ||");
}
public static void line2() {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}
That's all good and well, but I'd like to know how to import class Stewie from the first problem so I could use that in the second one without having to rewrite all the code. Can anyone help me out??
edit:re:importing a custom java class. I saw this before posting, but probably do not know enough about programming for it to be helpful to me at the moment. thank you though.
Have a look on how to create and use packages.
Because your methods in your first class are static declared, you can just call them within your second class.
package mypackage;
public class Stewie
{
public static void main(String[] args)
{
line();
qoute();
line2();
}
public static void line()
{
System.out.println("//////////////////////");
}
public static void qoute()
{
System.out.println("|| Victory is mine! ||");
}
public static void line2()
{
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}
The seconds class should also be in the same package. Then, because your methods declared in the first class are static, you can directly call them to your second class.
package mypackage;
public class Stewie2
{
public static void main(String[] args)
{
Stewie.line();
Stewie.qoute();
Stewie.line2();
}
}
Now in your second class, you can extend your code with further functionality.
Note: As already mentioned, this is allowed because your methods are static.
If your method, bellow wasn't static:
public void qoute()
{
System.out.println("|| Victory is mine! ||");
}
You would get the following error message:
Non-static method 'qoute()' cannot be referenced from a static context.
So be-careful and notice the difference.
Related
public class demo
{
public static void main(String[] args)
}
I just started to learn java, I have c++ experience, and the layout confuses me. for example if I declare a function in the demo class would that make it a function or method. also if I want to declare a class can I declare it outside the demo class or it must be inside the demo class.
thank you.
You have already declared it. The thing you are missing is your function body.
public static void main(String[] args)
should be
public static void main(String[] args){
//DO Some Stuff
}
Now here is some additional info:
The main function will be started whenever the application is started and the
String[] args
are the arguments that you are going to pass while starting the application.
You can declare as many functions as you want within your class
public class demo{
public static void main(String[] args){
//Do Some Stuff
}
private void someFunction(){
//Do Some Stuff
}
}
For more you can start learning some basics from the internet. There are tons of tutorials. Hope that helps. :)
you can write code like that
public class Emp{
//Instance variable or class level variable even variable as static
String id;
String name;
//static variable
static int count=0;
{
//non static block
}
static{
// static block
}
public Emp(){
//default constructor
}
//parameterized constructor
public Emp(String id, String name){
this.id=id;
this.name=name;
}
// Non-Static Method
public String getId(){
return id;
}
public String getName(){
return name;
}
//Main method
public static void main(String[] args){
//Instance of class
Emp emp=new Emp("1","Xyz");
System.out.println(emp.getId());
System.out.prinln(emp.getName());
}
}
In Java there are no functions. There are only methods.
You can declare methods inside of class definitions. And methods can be static or not static. Just like in C++.
Also there's no need for header files.
Example:
public class Demo {
// This is a constructor
public Demo() {
}
// This is a non-static method
public void method() {
}
// This is a static method.
// (It's also a special entry point to start the program)
public static void main(String[] args) {
}
}
I think it's safe to say that Java is much simpler than C++.
Ps. I capitalized Demo because according to Java's camel case convention, classes should start with an uppercase letter and methods with lowercase.
When I try to call the lyrics function in this code, "invalid method declaration; return type required" occurs.
I'm learning java and very very new to it. I'm confused how to define the function and call the function so that code may run.
public class Main {
public static void main(String[] args) {
}
public void lyrics() {
System.out.println("some lyrics here");
}
lyrics();
}
Normally, one can't just invoke a method randomly in the body of the code. However, there is something called an initialization block (this gets run in the body of the constructors of the object). I think an example might clarify. Like,
public class Main {
public static void main(String[] args) {
new Main(); // <-- instantiate an instance of Main
}
public void lyrics() {
System.out.println("some lyrics here");
}
{ // <-- this is an initialization block
lyrics();
}
}
The above uses the default constructor, we can add an explicit one. Like,
public Main() {
super();
System.out.println("In Main constructor");
}
Note how the output changes.
They can also be static (and run when the class is first referenced). Like,
public class Main {
public static void main(String[] args) {
}
public static void lyrics() {
System.out.println("some lyrics here");
}
static {
lyrics();
}
}
Your code is nearly correct. Your lyrics() method must be static if you want to call it inside main method because main method is static. Non static members cannot be accessed from static method (without creating an instance to invoke it).
public class Main {
public static void main(String[] args) {
lyrics();
}
public static void lyrics() {
System.out.println("some lyrics here");
}
}
You can invoke non-static methods from static method by creating an instance of the class containing non-static method inside your main method as mentioned in comments by Elliott Frisch.
new Main().lyrics();
Since you're learning Java, you must remember that the only way to call a non-static method from a static method is to have an instance of the class containing the non-static method.
Or you can simply make your function static. Also, you need to call the function inside the main block.
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
Happy coding!
The method should be declared as static and function call should be within main method. The correct code should look like as follows:
public class Main {
public static void main(String[] args)
{
lyrics();
}
public static void lyrics()
{
System.out.println("some lyrics here");
}
}
You need to move the call to lyrics() into the main method where code is actually executed. Where you have it now, the compiler is expecting a new method defination.
public class Main {
public static void main(String[] args) {
// execution begins here.
new Main().lyrics();
}
public void lyrics() {
System.out.println("some lyrics here");
}
}
EDIT: created new class Main class to avoid errors.
This seems really simple but I can't find a way to make this work in a program I'm making. I have two classes, and in one of them, I've created an array, that I want to access in another class.
This is the main class...
public class main {
public static void main(String[] args) {
boolean[] pixelValues;
pixelValues = [99];
}
}
Of course, I'm omitting a lot, but this is the code I'm having trouble with. There's a second class that goes a little like this...
public class pixelAssign {
public pixelAssign(posX, posY) {
main.pixelValues[ {some number} ] = {some value};
}
}
My problem is that I'm having trouble accessing the pixelValues array that I created in my main class from a function I made in the second bit of code. I've been getting an error in the second bit of code that say (1), says that variable can't be found, and (2), I'm missing an identifier (?). The code in this is a bit unfinished, but the function in the second bit would be called from the main class later.
Any help would be much appriciated!
Putting Paul's and SANM2009's answers together:
You should:
public class main {
static int[] pixelValues;
public static void main(String[] args) {
pixelValues = [99];
}
}
public class pixelAssign {
public pixelAssign(posX, posY) {
main.pixelValues[0] = 98;
}
}
That should work IF and only IF classes main and pixelAssign are in the same package! If not, pixelValues would not be visible.
If you want to hold with Java Good Practices:
First letter of classes are always Uppercase, so Main and PixelAssign
Often is a good practice to instante classes and use the objects. Otherwise you have to use static.
Set pixelValues as private or protected and create method:
public class Main {
private int[] pixelValues;
public Main() {
pixelValues = [99];
}
public void setPixelValue(int position, int value) {
pixelValues[position] = value;
}
public static void main(String[] args) {
Main main = new Main();
new PixelAssign(main);
}
}
public class PixelAssign {
public PixelAssign(Main main, posX, posY) {
main.setPixelValue(0, 98);
}
}
Why don't you create your function so that it accepts the array as a parameter. you can then send the array to classB from ClassA.
There seems to be another issue here.
pixelValues is meant to be an array of Boolean or some numbers (int)?
boolean[] pixelValues;
pixelValues = [99];
As for the function:
public pixelAssign(myArray, posX, posY) {
}
So when calling the function, use
pixelAssign(pixelValues, posX, posY)
Perhaps you have changed the code to make it more concise but it looks like you are simply declaring the array in the wrong scope. Could you change it to this;
public class main {
boolean[] pixelValues;
public static void main(String[] args) {
pixelValues = [99];
}
}
This question already has answers here:
Writing a function inside the main method - Java
(7 answers)
Closed 6 years ago.
Hello I am a want create method into main?
So this is code where i want crete method:
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}}
Java does not allow to create methods within a method. This is a general rule and NOT specific to main method.
Perhaps what you are looking for is to create additional static methods in your class:
public final class MyCommand {
private MyCommand() {
//
}
private static void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
releaseTheCatsOfWar();
}
}
Of course, it would be better to do:
public final class MyCommand {
private MyCommand() {
//
}
private void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
MyCommand that = new MyCommand();
that.releaseTheCatsOfWar();
}
}
you cannot create methods within methods in Java.
You can have it as an additional method of your class and call it form main.
public static void main is not a class, it's a method within class. And your class is input (by the way, you should start class name with an uppercase letter, like Input), in which you define method main, from which your program starts working.
Although it is entirely pointless, the closest way you would get to achieving your results is with the following:
public class Main {
private static interface MethodMaker{
void randomise();
}
public static void main(String[] args) {
MethodMaker methodMaker = new MethodMaker(){
#Override
public void randomise() {
// DO SOMETHING
}
};
methodMaker.randomise();
}
}
You have to put method outside main ,, and just call it in main ,, like this :-
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
Randomises();
}
public void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}
I am just learning how to create a subroutine/method in java, and I am having the problem that I can't call my method with the compiler thinking that my call (playGame();) is an attempted definition of a method of itself. So i get the error "invalid method declaration; return type required". As I am a beginner I am sure that it is a stupid mistake, but I have tried rewriting many times to fix it and I cannot figure it out.
public class GUI {
public static void main(String[] args){
}
public static void playGame() {
}
playGame();
}
You can only call a method from within another method, not from the body of a class. Move the line
playGame();
inside the main method:
public static void main(String[] args){
playgame();
}
Your method call should be inside another method , in this case main (or) You can call from playGame() also, but that would be recursion and may end up in infinite loop.
public static void main(String[] args){
playGame();
}
See Essentials of the Java Programming Language to learn more about how to write java program.
You can not call methods from class body directly the way you are doing. You need to call method playGame() from main method. Like :
public class GUI {
public static void main(String[] args){
playGame();
}
public static void playGame() {
// some statements
}
}
As you are new Start Reading Java Tutorial.