code::

import java.io.*;
import java.util.*;

public class Global{
	public static int flag=0;
}
interface filebackedfifoqueue
{
	void put(String msg);
	String get();
}

public class trying{
		public static void main(String args[])throws IOException{
			int ch=1;
			String msg;
			msg = new String();
			while(ch!=0)
			{
				BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
				System.out.println("\nMenu");
				System.out.println("\n0.EXit");
				System.out.println("\n1.Publisher");
				System.out.println("\n2.Subscriber");
				System.out.println("\nEnter your choice::");
				ch = Integer.parseInt(stdin.readLine());
				
				switch(ch)
				{
					case 0:
						break;
						
					case 1:
						Global.flag++;
						publisher();
						break;	

					case 2:	
						if(Global.flag>=1)
						//	subscriber();
							System.out.println("\nSubscriber part");
						else
							System.out.println("\nPublisher has not published anything yet!");
						break;
				}
			}
		}
		void publisher()
		{
			if(Global.flag==1)
			System.out.println("\nEnter the bounded size of the queue");	
		}
}

i need to use and increment a variable across 3 functions i.e:main,subscriber and publisher.
here is the code:
i am getting errors here:
The public type global must be declared in its own file.
Cannot make a static reference to the non-static method publisher() from the type trying

Recommended Answers

All 2 Replies

> The public type global must be declared in its own file.

Make sure the name of the file is the same as your public class/interface; also make sure a single file doesn't contain more than one top level public class / interface.

> Cannot make a static reference to the non-static method
> publisher() from the type trying

publisher() is an instance method and can only be invoked from another instance / non-static method.

Reading the stickies at the top of the forum is recommended.

thanks!!now no errors!!

import java.io.*;
import java.util.*;

class Global{
	public static int size=0;
}
interface filebackedfifoqueue
{
	void put(String msg);
	String get();
}
class publishers
{
	Queue queue=new Queue();
	int size;
	void initialize()throws IOException
	{
		BufferedReader stdin1=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("\nEnter the bounded size of the queue");
		size = Integer.parseInt(stdin1.readLine());
		queue.init(size);	
	}
	void put()
	{
		
	}
}

class Queue
{
	int size;
	int front=-1;
	void init(int a)
	{
		int size=a;
	}
}


public class trying{
		public static void main(String args[])throws IOException{
			publishers publisher1=new publishers();
			int ch=1,flag=0;
			String msg;
			msg = new String();
			while(ch!=0)
			{
				BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
				System.out.println("\nMenu");
				System.out.println("\n0.EXit");
				System.out.println("\n1.Publisher");
				System.out.println("\n2.Subscriber");
				System.out.println("\nEnter your choice::");
				ch = Integer.parseInt(stdin.readLine());
				
				switch(ch)
				{
					case 0:
						break;
						
					case 1:
						flag++;
						if(flag==1)
							publisher1.initialize();
						else
							publisher1.put();
						break;	

					case 2:	
						if(flag>=1)
						//	subscriber();
							System.out.println("\nSubscriber part");
						else
							System.out.println("\nPublisher has not published anything yet!");
						break;
				}
			}
		}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.